我们在使用 C# .Net Framework 1.1 开发的 Windows 服务中使用以下命令行:
net use z: \\myComputer\c$
该服务在“myComputer”上的本地管理员域帐户下运行。调试代码后,我们可以看到它没有返回任何错误,但“z:”驱动器从未被映射。我们已经从控制台应用程序中尝试了完全相同的代码,它可以正常工作。我们需要在服务中添加什么来完成这项工作?
我们正在使用的代码包含在下面。
问候,
塞尔吉奥
startInfo.FileName = "net";
startInfo.Arguments = string.Format(@"use {0}: \\{1}\{2}", driveLetter,
computerName, folder).Trim();
startInfo.UseShellExecute = false;
startInfo.RedirectStandardError = true;
proc.EnableRaisingEvents = false;
proc.StartInfo = startInfo;
proc.Start();
// If there is an error during the mapping of the drive, it will be read
// from the StandardError property which is a StreamReader object and
// be fed into the error output parameter.
using(StreamReader errorReader = proc.StandardError)
{
string standardError = string.Empty;
while((standardError = errorReader.ReadLine()) != null)
{
error += standardError + " ";
}
}
proc.WaitForExit();