27048

Rename a mapped drive with shell API

Question:

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.

Answer1:

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 <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/bb787868(v=vs.85).aspx" rel="nofollow">folder object</a> while all VB implementations seem to return a <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/bb787854(v=vs.85).aspx" rel="nofollow">folder2 object</a>. 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.

Answer2:

You should use the <a href="http://msdn.microsoft.com/en-us/library/aa365560.aspx" rel="nofollow">SetVolumeLabel</a> 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 <a href="http://msdn.microsoft.com/en-us/library/ms679360.aspx" rel="nofollow">GetLastError</a>.

Answer3:

<a href="http://msdn.microsoft.com/en-us/library/system.io.driveinfo.aspx" rel="nofollow">System.IO.DriveInfo</a> has a property <a href="http://msdn.microsoft.com/en-us/library/system.io.driveinfo.volumelabel.aspx" rel="nofollow">VolumeLabel</a> 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.

Recommend

  • Unable to start Selenium Safari WebDriver via python3
  • Is it possible to set breakpoints automatically (e.g. by regular expression) in VS2008?
  • How to identify user input help (/?) in batch file
  • how to check if a field is not unique
  • How do I call DotNetFactory from VBScript in a stand-alone .vbs file?
  • Reading Excel files in a locale independent way
  • Is there an advantage in using SOAP UI over REST-assured to automate REST Services for CI
  • nodejs tls session id
  • Can't connect using mysql_connect to Database in ipage.com hosting?
  • Creating a table in “MSWord” document out of a text in a .txt file
  • PHP MySQL generating unique random number
  • Parse returned C# list in AJAX success function
  • ssh2_scp_send() using php corrupts pdf
  • PHP PDF generation problem
  • Rails Route parameters in AngularJS
  • SEO friendly 301 redirect .htm to .aspx
  • Align Excel cell to center VB - xlCenter is not declared
  • Imports in __init__.py and `import as` statement
  • Escaping single quotes in JDBC with MySql
  • Autohotkey script running program with command line arguments
  • Visualizing a large matrix in matlab
  • Change attribute of custom directive
  • Ruby and class variables in inherit class
  • Insertion large number of Entities into SQL Server 2012 [duplicate]
  • What does certain JVM do after loading ByteCode into memory?
  • Should a web service response include empty values?
  • Adding Parent and Child Nodes in TreeView from Sql Server 2008
  • Most efficient way to move table rows from one table to another
  • GAE: Way to get reference to an HttpSession from its ID?
  • Spring boot 2.0.0.M4 required a bean named 'entityManagerFactory' that could not be found
  • What is the purpose of TaskExecutor in spring?
  • Alternative to overridePendingTransition() - Android
  • Swift: Switch statement fallthrough behavior
  • How to delay loading a property with linq to sql external mapping?
  • java.lang.NoClassDefFoundError: com.parse.Parse$Configuration$Builder on below Lollipop versions
  • Getting last autonumber in access
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • java string with new operator and a literal