Jump to content

More C# Help!


AwAke
 Share

Recommended Posts

Sorry for continiously coming here for help with C#, its the only forum I know which replies and with good advice.

Im trying to achieve the same effect as the AutoIt function 'DriveGetSerial(path)' but in C#.

So I read some forums and googled about and found this;

http://downloads.cfdan.com/csharp/usbserialnumber.cs.txt

So I added it as a class however VC 2008 returns six errors.

The errors being:

1. The type or namespace name 'ManagementObject' could not be found (are you missing a using directive or an assembly reference?)

2. The type or namespace name 'ManagementObject' could not be found (are you missing a using directive or an assembly reference?)

3. The type or namespace name 'ManagementObjectSearcher' could not be found (are you missing a using directive or an assembly reference?)

4. The type or namespace name 'ManagementObjectSearcher' could not be found (are you missing a using directive or an assembly reference?)

5. The type or namespace name 'ManagementObjectSearcher' could not be found (are you missing a using directive or an assembly reference?)

6. The type or namespace name 'ManagementObjectSearcher' could not be found (are you missing a using directive or an assembly reference?)

So its obvious that its something to do with the System.Management however it is in there as, 'Using System.Management'.

I have no idea what the reason could be with it as I merely copied and pasted the entire class.

Once again sorry for coming here for C# help, thanks in advance, AwAke.

Link to comment
Share on other sites

In your project (usually on the right side) there is a list of "Reference"s. Add System.Management to that (Right click > Add reference > System.Management).

... What would I do without you Manadar. Your always helping me and I can't thank you enough.

Thanks alot :) I really appreciate it.

Link to comment
Share on other sites

Uhm now there is another problem, perhaps its with the class?

Im calling the class like so;

USBDriveSerialNumber.USBSerialNumber usb = new USBDriveSerialNumber.USBSerialNumber();
string serial = usb.getSerialNumberFromDriveLetter(path);
MessageBox.Show(serial);

But now there is an error in the class which is appearing at runtime?

The error is with this bit of code;

ManagementObjectSearcher searcher1 = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDiskToPartition");
foreach (ManagementObject dm in searcher1.Get())

And the error is:

Context 0x33c3ff0 is disconnected. No proxy will be used to service the request on the COM component. This may cause corruption or data loss. To avoid this problem, please ensure that all contexts/apartments stay alive until the application is completely done with the RuntimeCallableWrappers that represent COM components that live inside them.

Link to comment
Share on other sites

Please search the related site for such information as it proves to be faster and more useful. Link, this query constructor requires full code trust and not partial one. So you'll need to request for such execution level using the project properties window. Also, read what exceptions are thrown in which cases to understand better what is the problem. Use SEH :)

Edit: I know I've seen once that you can request such code trust using Attribute program decoration. Search MSDN you'll find it, it's only valid outside any namespace scope, i.e. globally.

Edited by Authenticity
Link to comment
Share on other sites

Im not entirely sure about the exceptions, I'm guessing the, try & catch function?

Ive never used it or looked into it but I guess I should.

On regards to changing the trust settings.

I went into the project settings -> security -> Enabled ClickOnce Security Settings -> Selected This is a full trust application

Sorry if I have got the wrong end of the stick here, I dont understand much about this language.

Just trying to get into a project and learn as I go.

Thanks AwAk3e.

Edited by AwAke
Link to comment
Share on other sites

Please search the related site for such information as it proves to be faster and more useful. Link, this query constructor requires full code trust and not partial one. So you'll need to request for such execution level using the project properties window. Also, read what exceptions are thrown in which cases to understand better what is the problem. Use SEH :)

Edit: I know I've seen once that you can request such code trust using Attribute program decoration. Search MSDN you'll find it, it's only valid outside any namespace scope, i.e. globally.

Did you mean -> http://msdn.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute.aspx

Link to comment
Share on other sites

Nope, this one. Is the drive you're requesting it's serial number is in the form "C", "C:" or other A-Z letter in one of these forms? This topic got exactly the same code you've linked to so I think you'll find something there. Hope you'll got it fixed soon.

The drive that I'm requesting is given to me from a WndProc message and comes in the form per say as an example, G:\

The drives will always be USB Storage devices.

Ill take a look at the attributes and the other topic is the exact same code I have in the class im calling yet I still get that error.

Thanks for helping me out Authenticity, cheers Awake.

Link to comment
Share on other sites

Try this one, it's already defined but for some reason none of my drives return a USB drive serial. LOL

Yeah that works :| When in its own project it works fine. However if I copy the 'USBSerialNumber.cs' file into my project.

Add it as an existing class and call it, it gives me the error. O.O

Link to comment
Share on other sites

Try to be organized with your work. First make the solution folder (tick the checkbox to create a solution folder,) if you want. What is more important is to name the main project correctly because it'll be your new namespace tag for each code/class/interface file you add to that specific project. You can use the refactor tool but it's not perfect, you'll see this when you get the WPF.

I have nothing more to offer regarding your problem. :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...