views:

39

answers:

2

I am working on my HWID code. I recently tried converting this code from VB.net to C#. I have this one error which I can't seem to figure out.

'dsk' is a 'variable' but is used as a 'method'

Here is my code

    string returnString = null;
    string systemDisk = Environment.GetEnvironmentVariable("windir", EnvironmentVariableTarget.Machine);
    if (systemDisk != null)
    {
        ManagementObject dsk = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + systemDisk.Substring(0, 2) + "\"");
        dsk.Get();
        returnString = dsk("VolumeSerialNumber");
    }
    return returnString;
+1  A: 

dsk must implement indexing, which uses the same syntax as a method call in VB.NET. However, in C# it uses its own syntax (var[index]). As such try this:

returnString = dsk["VolumeSerialNumber"]; 
Graphain
+2  A: 

In c# we use [] for indexers instead of (). () Is usually (always?) used to call a method/delegate.

You need to change the line

returnString = dsk("VolumeSerialNumber");

to

returnString = dsk["VolumeSerialNumber"];
TheEvilPenguin
Penguin only has 100 rep and this took no time so I'm indifferent, but typically the first answer when all answers are otherwise correct/identical is accepted.
Graphain