如何让.NET代码将Z:驱动器映射到帐户SVNdatamgmt的UNC路径?
我正在尝试将本地驱动器映射到.NET控制台应用程序中的网络UNC路径。代码似乎在命令行中用于服务帐户(#2和#3)以及我的凭据(#4)。但是当使用.NET源代码从Console应用程序运行时,服务帐户不起作用(#5),但我的凭据确实有效(#6)。
昨晚,我注意到我收到了错误(#1)。等了30分钟后,它奏效了。所以你可以忽略#1。我想我会提到它,以防它提供了发生了什么的线索。
控制台应用程序在Windows Server 2008框中以管理员身份运行。 SVCdatamgmt和macgyver都是此计算机上的管理员。这些命令也在同一台机器上运行。
=============================================== =========================
1。)昨晚不起作用:
C:> net use z: \\uwhc-sas2\SASHIMC /USER:SVCdatamgmt thepassword
System error 1909 has occurred.
The referenced account is currently locked out and may not be logged on to.
=============================================== =========================
2。)等了30分钟然后这个工作(没有域名):
C:> net use z: \\uwhc-sas2\SASHIMC /USER:SVCdatamgmt thepassword
The command completed successfully.
=============================================== =========================
3。)这也适用于(使用域名):
C:> net use z: \\uwhc-sas2\SASHIMC /USER:UWHIS\SVCdatamgmt thepassword
The command completed successfully.
=============================================== =========================
4。)这也适用于我的凭据:
C:> net use z: \\uwhc-sas2\SASHIMC /USER:macgyver thepassword
The command completed successfully.
=============================================== =========================
5。)映射驱动器的.NET代码。 SVCdatamgmt凭据不起作用。
public static void MapNetworkDriveToUNC()
{
var command = @"net use " + mapDrive + " " + uncPath + " " + uncUser + " " + uncPass;
ExecuteCommand(command, 10000);
}
public static void UnmapNetworkDriveToUNC()
{
var command = "net use " + mapDrive + " /delete";
ExecuteCommand(command, 5000);
}
=============================================== =========================
6。)映射驱动器的.NET代码。我的凭据有效(macgyver)
- 与#5相同的代码 -
=============================================== =========================
仅供参考:在运行上述每个命令之前,我必须使用此代码断开(取消映射)驱动器...
C:\>net use z: /delete
z: was deleted successfully.
答案 0 :(得分:1)
我一直在寻找从C#映射UNC路径到网络驱动器的命令,我实现了这个并且效果很好。它太晚了,但将来可能对某人有所帮助:
public static bool MapDrivetoUNC(string DriveName, string Path)
{
try
{
bool isMapped = true;
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
//p.StartInfo.RedirectStandardError = true;
//p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "net.exe";
p.StartInfo.Arguments = " use " + DriveName + ": " + '"' + Path + '"';
p.Start();
p.WaitForExit();
//str errMsg = p.StandardError.ReadToEnd();
// similar for erroutput ..
isMapped = true;
}
catch(Exception ex)
{
Utility.logError(ex);
isMapped = false;
}
return isMapped;
}