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 IamaSherpa 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
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==
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top