0

I want to change this label:

that

In the above picture I changed it manually without any permissions.

I tryed the following as said in this thread but it gives an System.UnauthorizedAccessException

public void setVolumeLabel(String oldName, string newLabel){
        DriveInfo[] allDrives = DriveInfo.GetDrives();
        foreach (DriveInfo d in allDrives){
            if (d.IsReady && d.Name.Equals(oldName)){
            d.VolumeLabel = newLabel;
        }
}

So the VolumeLabel property is not what I'm looking for. Then I read this post, but can't import the Shell32.dll for some reason, it says Axlmp.exe cannpt be found.

Tried also with SetVolumeName(). It returns nonzero numbers, but doesn't change de VolumeName.

[DllImport("kernel32.dll", SetLastError=true)]  
public static  extern bool SetVolumeLabel(String letter, String label);

[DllImport("kernel32.dll")]
public static extern uint GetLastError();
Community
  • 1
  • 1
tec
  • 999
  • 3
  • 18
  • 40

2 Answers2

1

As you are (presumably) logged in with administrator rights you can edit this from within Explorer. But your C# software does not run as administrator unless you specifically say so by one of the following methods:

1) Running VS as administrator

2) Run your application from within Explorer as administrator

3) Configure your application to run as administrator through its manifest

LordWilmore
  • 2,829
  • 2
  • 25
  • 30
0

As the documentation for the DriveInfo.VolumeLabel property states very clearly, an UnauthorizedAccessException can be thrown for one of the following reasons:

  • The volume label is being set on a network or CD-ROM drive.
  • Access to the drive information is denied.

The second one was a good assumption; it is the one that LordWilmore made in his answer. However, you've indicated that is not the case.

This leaves the first one, which makes sense, considering you've indicated in a comment to the question that drive J: is, in fact, a network drive. You cannot use this mechanism to change the label of a network drive.

To do this, you need to use a COM interface. You will query the shell to obtain a Folder2 object representing the network drive you wish to manipulate. Then, you will use this object's Self property to retrieve the folder's corresponding FolderItem object. Finally, you can directly set the Name property for that object.

Here is the solution in C++, and you already found the solution in C# and VB 6. However, you claim that you were unable to add a reference to the "Microsoft Shell Controls and Automation" library as Syberdoor suggested. I'm not sure why not. The error message you're getting makes very little sense. AxImp.exe is part of the standard .NET Framework install. You are possibly doing it wrong. Try:

  1. Right-click the project in the Solution Explorer
  2. Select "Add Reference"
  3. In the "Add Reference" dialog, switch to the "COM" tab
  4. Check "Microsoft Shell Controls and Automation"

I guess if you somehow do not have the Windows SDK installed, you could be missing AxImp.exe, and Visual Studio could be failing to create the stub assembly that allows you to use the typelib. I cannot imagine how that could possibly happen. If you have Visual Studio installed, you should have gotten the SDK.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • I added the library as you said. A Shell32 file appears in the reference folder, but give me the axlpm.exe error. I dont know if I have Windows SDK installed, at least it is not present in Control Panel programs. I'm using SharpDevelop. Does any computer need to have this SDK for change the network resource label (like my case)? – tec Dec 21 '16 at 21:59
  • Well, jeez. You should have mentioned you were using SharpDevelop in the question. I was assuming Visual Studio, which would have come with the SDK install. You'll need to install it separately for SharpDevelop. [Download the Windows 7 SDK here](https://www.microsoft.com/en-us/download/details.aspx?id=8279), that should work for your purposes. @tomy – Cody Gray - on strike Dec 22 '16 at 04:22
  • But once I intalled Windows SDK, would the app work in any pc ? Or any pc has to have the SDK ? – tec Dec 22 '16 at 07:47