Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Map a Drive

Status
Not open for further replies.

ChadJK

IS-IT--Management
Nov 10, 2005
14
0
0
US
How can I map a network share as a drive letter using C#. Normally you'd use a "NET USE ..." from the command prompt, but there has to be a programmatic way to do it in C#.
 
You still can use net use programmatically.

Code:
// for the Process()
using System.Diagnostics;

string mCommand = @"/c net use q: \\server\share";

      Process myProcess = null;
      
      try
      {  
        myProcess = new Process(); 
        myProcess.StartInfo.FileName = "CMD.EXE"; 
        myProcess.StartInfo.Arguments = mCommand;
        myProcess.StartInfo.CreateNoWindow = true;
        myProcess.StartInfo.UseShellExecute = false;
        myProcess.StartInfo.RedirectStandardError = true;
        myProcess.Start();
				
					        
        if(!myProcess.WaitForExit(60000))
        {
          // do error failure stuff
        }
      }
      catch(Exception ex)
      {
        // more error stuff        
      }

Tested this several times and it worked fine. Let me know if it does the trick.

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
I don't think there is a direct way, actually. You can use WinApi calls, though.


I'll also sound a warning that drive mappings are specific to a user account... so any mappings you create will likely only exist for the account your apps is running under.



Thomas D. Greer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top