I need to map a network drive from within a .NET application. I'm going to need to use an AD Username and Password to authenticate. Usually I just use a batch file with the net use
command. How do I do this from within C# or VB.NET code?
Asked
Active
Viewed 4.9k times
10

Ben McCormack
- 32,086
- 48
- 148
- 223
-
Why are you mapping a drive? To copy files? – Ed B Jul 07 '10 at 21:15
-
@Ed B yeah, after thinking about it for a few more seconds, we're realizing we're going to approach this differently. – Ben McCormack Jul 07 '10 at 21:17
-
Ok..what I do is share a folder on the target machine..and do impersonation to save a file on another machine. I can control the permissions of who writes to the folder, when setting up the share. – Ed B Jul 07 '10 at 21:33
2 Answers
15
Have you looked at this?
http://www.codeguru.com/csharp/csharp/cs_network/windowsservices/article.php/c12357
Also, you could just use net.exe via Process.Start()
and pass it the parameters you've always used in the code below:
System.Diagnostics.Process.Start("net.exe", "use K: \\\\Server\\URI\\path\\here");
This can also be used without a drive letter and then accessed through the UNC path.
System.Diagnostics.Process.Start("net.exe", @"use @"\\Server\URI\path\here");
System.IO.File.Copy(@"\\Server\URI\path\here\somefile.abc", destFile, true);
-
1Hi, nice work, if you wrap it in a function you could go: `private void MapDrive(string driveLetter, string UNCPath) { ProcessStartInfo processStartInfo = new ProcessStartInfo( "net.exe", string.Format(@"use {0}: {1}", driveLetter) ); Process process = Process.Start(processStartInfo); }` – Elken Feb 28 '14 at 10:57
-
1I wanted to throw in here that you can issue the net use command *without* a drive name then access the path via the UNC path. This way you don't have to worry about what drives a user might have mapped already. – Tim Coker Apr 07 '15 at 19:15
-
1@Tim Coker The part about not using a drive letter is brilliant. Wish I knew that little gem years ago. – MatthewD Jun 28 '19 at 15:12
0
Heres some code that you should find to be a bit more reliable than just shelling out to the console.
''' <summary>
'''
''' </summary>
''' <param name="driveLetter"></param>
''' <param name="uncName"></param>
''' <remarks>This was hand tested. We cannot automate because it messes with the OS</remarks>
Sub MapDrive(ByVal driveLetter As Char, ByVal uncName As String)
Dim driveLetterFixed = Char.ToLower(driveLetter)
If driveLetterFixed < "a"c OrElse driveLetterFixed > "z"c Then Throw New ArgumentOutOfRangeException("driveLetter")
If uncName Is Nothing Then Throw New ArgumentNullException("uncName")
If uncName = "" Then Throw New ArgumentException("uncName cannot be empty", "uncName")
Dim fixedUncName As String = uncName
'This won't work if the unc name ends with a \
If fixedUncName.EndsWith("\") Then fixedUncName = fixedUncName.Substring(0, fixedUncName.Length - 1)
Dim oNetWork As New IWshRuntimeLibrary.IWshNetwork_Class
Try 'This usually isn't necessary, but we can't detect when it is needed.
oNetWork.RemoveNetworkDrive(driveLetter, True, True)
Catch ex As Runtime.InteropServices.COMException
'Ignore errors, it just means it wasn't necessary
End Try
oNetWork.MapNetworkDrive(driveLetter, fixedUncName, True)
End Sub
http://clrextensions.codeplex.com/SourceControl/changeset/view/55677#666894

Jonathan Allen
- 68,373
- 70
- 259
- 447