How to Get VolumeSerial Of Harddisk?

  • Thread starter Thread starter Arif Çimen
  • Start date Start date
A

Arif Çimen

Hi everybody
I need to get serial number of harddist drive (C:\).

Here I tried some codes.

//**************
using System;

using System.Runtime.InteropServices;

namespace HanleyUretim

{

/// <summary>

///

/// </summary>

public class ManagerClass

{

public ManagerClass()

{

//

// TODO: Add constructor logic here

//

}

//public SerialReader sr=new SerialReader();


unsafe public string GetSerial()

{

//char[]

//long serial1=0;//=(int*)new int();

string lpVolumeNameBuffer="Test";//new string(;

char[] root=new char[3];

root[0]='C';

root[1]=':';

root[2]='\\';

//long *serial=&serial1;

long serial=0;

SerialReader.GetVolumeInformation(root,lpVolumeNameBuffer,10,&serial,0,0,"FAT",10);

string result=Convert.ToString(serial);

return result;//here the result is 255 (NOT the serial).Why?

}




}

public class SerialReader

{

[DllImport("Kernel32.dll")]

unsafe public static extern bool GetVolumeInformation(

char []lpRootPathName,

string lpVolumeNameBuffer,

long nVolumeNameSize,

long *lpVolumeSerialNumber,

long lpMaximumComponentLength,

long lpFileSystemFlags,

string lpFileSystemNameBuffer,

long nFileSystemNameSize

);

}

}

//**************

I manage the GetVolumeInformation function in C++, but not in C#.

Thanks for any help.
 
Remember long's are 64 bit in .NET, change them to int, when running on XP
or higher, you better use the System.Management classes and WMI's class
Win32_PhysicalMedia instead of PInvoke.

Willy.
 
Back
Top