How To : Change Drive Name Or Label In C#

driv
Changing Drive things are pretty easy with kernel32 so what we are going to do is just change the name or label , okay okay i know it's called label and not name most of the times :D . first of all let's design the Form .

Add two text boxes and two labels and a button and make your look like this(if you believe in my UI design)
ui des
Now coming to the coding , hmm we need to add kernel32 to the project so add the namespace using System.Runtime.InteropServices;  , Now import the kernel32.dll to your project by adding these codes under or on top of 
public Form1()
        {
            InitializeComponent();
        }
The codes(kernel32)
        [DllImport("kernel32")]
        static extern long SetVolumeLabelA(string lpRootName, string lpVolumeName);
After that go to your button(rename) click codes and add these
            long lab;
            lab = SetVolumeLabelA(textBox1.Text, textBox2.Text);
Now test your project , if it didn't work for you please let me know by commenting on this post or else contact me via the contact page I hope explanations are not needed for this simple codes , Thank you.

3 comments

Post a Comment

Note: Only a member of this blog may post a comment.