Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How we can Get Logical drive Setting and Setting the Volume Label Using Win32 System Services

Author: DevASP
Download Source Code : 636_Logic drives and Volume Labels src.zip

You can query Windows for the drive letters that are currently in use. Windows returns that information to you either as a bit array or as a set of strings.

This can be done in two ways.Win32 System Services provides 2 API’s for this.

1-     GetLogicalDrives

2-     GetLogicalDriveStrings

Signature of these functions looks like this.

GetLogicalDrives: 

                            Returns a bit array containing drives in use

 

 

DWORD GetLogicalDrives(void);

 

Returns a 32-bit bit string.

GetLogicalDriveStrings:     

 Returns all the logical drive strings in use

 

 

DWORD GetLogicalDriveStrings(DWORD bufferSize,LPTSTR buffer);

 

bufferSize          The size of the buffer

buffer                Buffer to hold the strings

Returns 0 on error, or the length of the string in bytes.

 

The GetLogicalDrives function returns a 32-bit value that contains one bit for each drive letter currently in use. Bit zero represents drive A. The code given below looks at each bit up through bit 25 and prints out the corresponding drive letter for each bit that contains a one.

 

 

DWORD drives;

      int x;

      drives = GetLogicalDrives();

      CString str;

      str.Format("Logical drives on this machine:\n");

      //cout << "Logical drives on this machine: ";

 

      for (x=0; x<26; x++)

      {

            if (drives & 1!=0)

            {

           

            CString fmt;

            char ch=(char)('A'+x);

            fmt.Format("%c",ch);

            str+=fmt;

            }

            drives = drives >> 1;

      }

      MessageBox(str); 

 

This will display you the All the drives on a Message Box in your Disk Drive.The GetLogicalDriveStrings function returns a set of strings, one for each drive letter currently in use. The strings are embedded in a single character array with NULL characters between them. Two NULL characters follow the final string to mark the end of the set. The program simply Display all these strings in a Message Box.If the return value from GetLogicalDriveStrings is zero, it indicates failure, and you should use GetLastError to determine the problem. If the return value is greater than the size of the buffer, you should resize your buffer and call GetLogicalDriveStrings again.

 

 

 

DWORD len;

      char buffer[1000];

      char *p;

 

      len = GetLogicalDriveStrings(1000, buffer);

      //cout << "Logical drives on this machine: \n";

      CString str="Logical drives on this machine: \n";

 

      for (p=buffer; *p != '\0'; p++)

      {

            //cout << p << endl;

            CString fmt;

            fmt.Format("%s\n",p);

            str+=fmt;

            while (*p != '\0') p++;

      }

      MessageBox(str);

 

 

This will display all the Logical drives strings on a message box.

Setting the Volume Label:

    You can change the label on a volume, provided your account

has sufficient privilege to do so, using the SetVolumeLabel function.

SetVolumeLabel:                    

   Changes the label on a volume

  

 

BOOL SetVolumeLabel(LPTSTR rootName,LPTSTR volumeName);

 

rootName          The name of the logical drive

volumeName      New name for the volume

Returns TRUE on success.

 

The volume name is typically first set when the administrator or user formats the drive. For example, if you format a new floppy disk with the File Manager, you can set its volume label at that time. You can also change a volume label from the File Manager or with the label command from the command line. The SetVolumeLabel function simply provides a programmatic mechanism for doing the same thing. If the function does not succeed, GetLastError contains the appropriate error code.

 

 

 

BOOL success;

      char volumeName[]="TEST";

 

      success = SetVolumeLabel("c:\\", volumeName);

      if (!success)

            MessageBox("Cant change Label");

 

The above code will change the Volume Label of “C” drive to Test. 

Article Comments
good article.
But i want to get volume name and not only drive letter.
eg. NewVolume(C:)
I want to get NewVolume

Posted on 2/18/2010 3:00:11 AM by Swapnil Surawase

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Getting free Disk space Using Win32 System Services

Disclaimer - Privacy
© 2002-2017 DevASP.net