How to Get VolumeSerial Of Harddisk?

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.
 
W

Willy Denoyette [MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top