Click to See Complete Forum and Search --> : A Beginner Problem


AwAk3e
October 18th, 2009, 12:38 PM
Hello, I have just started to learn C# and come from a type-safe language, AutoIt. So I'm just coming to terms with classes etc and I was trying to achieve a function in AutoIt known as DriveGetSerial(path) which will allow me to get the serial of a USB Storage device.

So after googling about I found this;
http://downloads.cfdan.com/csharp/usbserialnumber.cs.txt

However it gives me six errors, all about the System.Management.

The errors are:

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?)

However as seen in the class file I have added, 'Using System.Management' and yet it yields errors so in hope of a solution I have came to these forums.

Does anyone know why this is happening? Thanks in advance, AwAk3e.

Shuja Ali
October 18th, 2009, 01:32 PM
Welcome to the Forum. C# is a type safe language too. What you need to do is add a reference to System.Management.DLL file in your C# project.

AwAk3e
October 18th, 2009, 04:01 PM
Ty for your response Shuja Ali, the code now runs but I get another error.

The code taken from http://downloads.cfdan.com/csharp/us...lnumber.cs.txt I have in a seperate class file in my project.

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.

I read some information at MSDN about the project needing full trust, so I went into the project settings -> security -> Enabled ClickOnce Security Settings -> Selected This is a full trust application

Yet the class still yields the error. I have no idea what is causing it.
Thanks in advance, AwAke.

AwAk3e
October 19th, 2009, 08:25 AM
Although I set the global settings to Full Trust the class is has the modifer Partial could this be the problem?

I tried removing the partial from both my main form and my designer form. So it now looks like this;

Main.cs -> public class PenDriveBackup : Form {
code is here
}

Main.Designer.cs -> public class PenDriveBackup {
code is here
}

However when removing the partial from both of these I get this error:
1. The namespace 'PenDriveBackupProgram' already contains a definition for 'PenDriveBackup'


How would I resolve this?