Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在.NET中映射需要用户名和密码的网络驱动器?_C#_.net_Windows_Vb.net - Fatal编程技术网

C# 如何在.NET中映射需要用户名和密码的网络驱动器?

C# 如何在.NET中映射需要用户名和密码的网络驱动器?,c#,.net,windows,vb.net,C#,.net,Windows,Vb.net,我需要从.NET应用程序中映射网络驱动器。我需要使用广告用户名和密码进行身份验证。通常我只使用带有netuse命令的批处理文件。如何从C#或VB.NET代码中执行此操作?您看过这个吗 此外,您还可以通过使用net.exe并将您在下面代码中始终使用的参数传递给它: System.Diagnostics.Process.Start(“net.exe”,“使用K:\\\\Server\\URI\\path\\here”); 这也可以在没有驱动器号的情况下使用,然后通过UNC路径访问 System

我需要从.NET应用程序中映射网络驱动器。我需要使用广告用户名和密码进行身份验证。通常我只使用带有
netuse
命令的批处理文件。如何从C#或VB.NET代码中执行此操作?

您看过这个吗

此外,您还可以通过使用net.exe并将您在下面代码中始终使用的参数传递给它:

System.Diagnostics.Process.Start(“net.exe”,“使用K:\\\\Server\\URI\\path\\here”);
这也可以在没有驱动器号的情况下使用,然后通过UNC路径访问

 System.Diagnostics.Process.Start("net.exe", @"use @"\\Server\URI\path\here");
 System.IO.File.Copy(@"\\Server\URI\path\here\somefile.abc", destFile, true);

这里有一些代码,您应该会发现它们比直接发送到控制台更可靠

''' <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
“”
''' 
''' 
''' 
''' 
“这是手工测试的。我们无法自动化,因为它会干扰操作系统
子映射驱动器(ByVal driveLetter作为字符,ByVal uncName作为字符串)
Dim driveLetterFixed=Char.ToLower(driveLetter)
如果driveLetterFixed<“a”c或Else driveLetterFixed>“z”c,则抛出新的ArgumentOutOfRangeException(“driveLetter”)
如果uncName为Nothing,则抛出新的ArgumentNullException(“uncName”)
如果uncName=“”,则抛出新ArgumentException(“uncName不能为空”,“uncName”)
Dim fixedUncName作为字符串=uncName
'如果unc名称以\
如果fixedUncName.EndsWith(“\”),则fixedUncName=fixedUncName.Substring(0,fixedUncName.Length-1)
Dim-Network作为新的IWShurantimeLibrary.IWshNetwork\u类
尝试“这通常是不必要的,但我们无法检测何时需要它。
OneWork.RemoveNetworkDrive(driveLetter,True,True)
Catch ex作为Runtime.InteropServices.COMException
“忽略错误,这只是意味着没有必要
结束尝试
OneWork.MapNetworkDrive(driveLetter,fixedUncName,True)
端接头

为什么要映射驱动器?复制文件?@Ed B是的,在考虑了几秒钟之后,我们意识到我们将以不同的方式处理这个问题。好的。我要做的是在目标计算机上共享一个文件夹。然后在另一台计算机上进行模拟以保存一个文件。设置共享时,我可以控制写入文件夹的人的权限。您好,做得好,如果您将其包装在一个函数中,您可以转到:
private void MapDrive(string driveLetter,string UNCPath){ProcessStartInfo ProcessStartInfo=new ProcessStartInfo(“net.exe”,string.Format(@“use{0}:{1}”,driveLetter));Process Process=Process.Start(processStartInfo);}
我想在这里加上一句,您可以发出不带驱动器名的net use命令,然后通过UNC路径访问路径。这样,您就不必担心用户可能已经映射了哪些驱动器。@Tim Coker关于不使用驱动器号的部分非常精彩。但愿我几年前就知道那颗小宝石。