0

如何使 .NET 代码将 Z: 驱动器映射到帐户 SVNdatamgmt 的 UNC 路径?

我正在尝试将本地驱动器映射到 .NET 控制台应用程序中的网络 UNC 路径。该代码似乎可以从服务帐户(#2和#3)和我的凭据(#4)的命令行工作。但是,当使用 .NET 源代码从控制台应用程序运行时,服务帐户不起作用(#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 code that maps drive. SVCdatamgmt credentials do not work.

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 code that maps drive. My credentials work (macgyver)

-- same code as #5 --

========================================================================
FYI: before running each command above, I have to disconnect (unmap) the drive using this code...

C:\>net use z: /delete

z: was deleted successfully.
4

1 回答 1

1

I was looking for the command to map UNC Path to Network Drive from C# and I implemented this and works great. Its too late but may be helpful to someone in future:

  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;
  }
于 2013-07-11T00:41:07.103 回答