12

我们在使用 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();  
4

5 回答 5

18

来自http://msdn.microsoft.com/en-us/library/ms685143.aspx

必须访问远程资源的服务(或在不同安全上下文中运行的任何进程)应使用通用命名约定 (UNC) 名称来访问资源。服务必须具有适当的权限才能访问资源。如果服务器端服务使用 RPC 连接,则必须在远程服务器上启用委派。

驱动器号对系统来说不是全局的。每个登录会话都会收到自己的一组从 A 到 Z 的驱动器号。因此,重定向的驱动器不能在不同用户帐户下运行的进程之间共享。此外,服务(或在其自己的登录会话中运行的任何进程)无法访问在不同登录会话中建立的驱动器号。

服务不应通过映射驱动器号直接访问本地或网络资源,也不应在运行时调用 net use 命令映射驱动器号。

于 2008-11-11T19:58:07.697 回答
1

您无法从 Windows 服务(包括注册表中的 HKEY-CURRENT-USER)访问用户属性,因为该服务不会以登录用户身份运行。

映射的驱动器是用户设置的一部分,因此您不能将它们用作服务,除非您手动挖掘注册表中的用户属性,映射服务中的驱动器等。这很痛苦。

您可能想要尝试做的是询问有关如何让您的服务执行登录序列的问题(可能是一些 .EXE)。这可能对你有用。

希望这会有所帮助,艾伦。

于 2008-11-11T20:07:23.327 回答
0

I was doing something similar to log in to a remote server, but without the mapped drive part. I don't like using mapped drives; in programs that is, I use subst for convenience all the time. Anyway, I just had to make sure to include

use \\server\c$ /user:admin password

or whatever your user/password is that has access to the remote server, then it doesn't matter what the service is logged on as.

于 2012-07-25T19:11:19.750 回答
0

您可能需要指定用于登录的帐户。在命令提示符下键入net use /?以获取使用命令进行设置的帮助。

于 2008-11-11T19:49:35.093 回答
0

我怀疑这是因为该服务没有在本地用户的上下文中运行。我记得,您可以将多年前的 Windows 服务配置为“与桌面交互”或类似的东西。

于 2008-11-11T19:51:58.647 回答