3

How can I change the friendly name of a mapped drive using the Windows shell API and C#? My actual problem is that I am dealing with a disconected network drive without a UNC path, so the only way to rename it is from Explorer, but I want to do that programmatically.

Gabe
  • 84,912
  • 12
  • 139
  • 238
apaka
  • 31
  • 2
  • possible duplicate of [C# How to change the drive letter of the CDROM from D: to Z:](http://stackoverflow.com/questions/5084297/c-how-to-change-the-drive-letter-of-the-cdrom-from-d-to-z) – Arsen Mkrtchyan Aug 16 '11 at 13:59
  • 1
    Not a duplicate; this is a different question. OP wants to know how to rename the "friendly name" of the mapped drive, not change the drive letter. – James Johnston Aug 16 '11 at 15:33
  • duplicate of [How to rename or relabel a Network Drive label](http://stackoverflow.com/questions/4460390/how-to-rename-or-relabel-a-network-drive-label) – Luke Aug 16 '11 at 17:22
  • It's not a duplicate , because I tested it long time ago, it doesent works. I need something that does exactly like right click-> rename. – apaka Aug 16 '11 at 17:48
  • There's an outside chance that ShellExecute would work, using "rename" as the operation and providing the new name in lpParameters. I have no idea what you'd need to pass in lpFile, though: "the fully qualified parse name" doesn't mean much to me. – Harry Johnston Aug 17 '11 at 02:59
  • The question I linked to does exactly that and works. – Luke Aug 17 '11 at 18:23

3 Answers3

4

I had a similar problem and solved it using the following code:

Shell32.Shell shell = new Shell32.Shell();
((Shell32.Folder2)shell.NameSpace("X:")).Self.Name = "Friendly Label";

With a reference to COM --> Microsoft Shell Controls and Automation. It is basically the C# representation of an old VBS Code I had

Set oShell = CreateObject("Shell.Application")
oShell.NameSpace("X:").Self.Name = "Friendly Label"

The difference however is that the C# implementation of NameSpace for some reason returns a folder object while all VB implementations seem to return a folder2 object. Only the folder2 has the 'Self' property, so the additional cast is needed.

Also, as was pointed out in one of the comments, this only works within an STA apartment, so the Main() method has to be decorated with [STAThread].

I hope it's not bad practice to answer such old questions, but I was quite frustrated to not find a solution to this anywhere.

Syberdoor
  • 2,521
  • 1
  • 11
  • 14
  • 1
    It is in no way bad practice to contribute valuable answers to old, un-answered questions. :) – Henrik Aasted Sørensen Dec 11 '14 at 09:03
  • 1
    Note that this only works in an STA apartment, so to use this you must decorate the `Main()` method with `[STAThread]` – Matthew Watson Aug 28 '15 at 07:55
  • This works pretty well for setting a new label. Unfortunately it doesn't seem to be able to reset the network drive to its original label. At least not with null or an empty string. In the Windows explorer it can be reset by renaming it to an empty string. Any ideas how to do this with this code? – CodeX Jan 31 '17 at 02:44
  • @CodeX I could not reproduce this. In my tests it works in both vbscript and c# by providing an empty string – Syberdoor Jan 31 '17 at 08:51
  • I receive a `InvalidCastException` with C#, any ideas why I can't cast it to a `Shell32.Folder2`? – Matt Jan 08 '19 at 19:44
0

You should use the SetVolumeLabel API.

Basically, the drive's "name" that you're referring to is called the Volume Label. You could P/Invoke the API and change it that way.

To get extended error information, you can use GetLastError.

JRL
  • 76,767
  • 18
  • 98
  • 146
Daniel Walker
  • 760
  • 6
  • 11
  • That doesent work. The device doesen let me to change it's volume label. – apaka Aug 16 '11 at 15:52
  • Could you be more specific? I've used the API, so I know it works. Are you getting an error? – Daniel Walker Aug 16 '11 at 15:54
  • 1
    By name I was refering to name which explorer displays. The volume name is't the problem and it can't be changed it is hardcoded into devicies driver. I'm interested in renamin 'Disconnected network drive' into something else. – apaka Aug 16 '11 at 16:59
0

System.IO.DriveInfo has a property VolumeLabel that lets you change the label on your volumes. Check the exceptions and remarks on VolumeLabel to see the requirements for renaming a volume.

It looks like you can't outright rename the UNC unless you map it as a network drive. You could also create a shortcut to the UNC and rename that as well.

The Moof
  • 794
  • 4
  • 10