简体   繁体   English

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

[英]How do I map a network drive that requires a username and password in .NET?

I need to map a network drive from within a .NET application. 我需要从.NET应用程序中映射网络驱动器。 I'm going to need to use an AD Username and Password to authenticate. 我将需要使用AD用户名和密码进行身份验证。 Usually I just use a batch file with the net use command. 通常我只使用带有net use命令的批处理文件。 How do I do this from within C# or VB.NET code? 如何在C#或VB.NET代码中执行此操作?

Have you looked at this? 你看过这个吗?

http://www.codeguru.com/csharp/csharp/cs_network/windowsservices/article.php/c12357 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: 此外,您可以通过Process.Start()使用net.exe,并将以下代码中始终使用的参数传递给它:

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. 这也可以在没有驱动器号的情况下使用,然后通过UNC路径访问。

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

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 http://clrextensions.codeplex.com/SourceControl/changeset/view/55677#666894

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用.Net / Parse.com发送API凭据和用户名/密码? (PHP到C#) - How do I send API credentials and username / password with .Net / Parse.com? (PHP to C#) 如何从 Windows 服务 map 网络驱动器 - How can I map a network drive from a Windows Service 使用TLS安全性在.Net中映射网络驱动器 - Map Network Drive in .Net with TLS security 如何访问需要身份验证的共享网络文件夹? - How do I access a shared network folder which requires authentication? 如何在Web服务请求.net核心上获取网络凭据用户名和密码? - how to can get Network Credential username and password on webservice request .net core? 如何从网址获取用户名和密码 - How do I get the username and password from a URL 如何使用用户名和密码连接LotusNotes服务器 - How do I connect LotusNotes server using username and password 如何在令牌生成时强制执行用户名和密码,或者这是不好的做法 - How do I enforce username and password on token generation or is it bad practice 如何验证 Azure AD 中的用户名和密码? - How do I validate a username and password in Azure AD? 如何在C#中获取网络驱动器大小(没有“映射驱动器”) - How to get Network drive size in C# (without “map drive”)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM