C# 如何从windows服务中映射网络驱动?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/792868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does one map network drive from windows service?
提问by Ahmed Atia
I'm trying to map network drive from windows service, I use batch file for executing the following command
我正在尝试从 Windows 服务映射网络驱动器,我使用批处理文件来执行以下命令
NET USE U: \192.168.55.6\folder password
While executing batch file either in service constructor or in onstart event, drive is not mapped?
在服务构造函数或 onstart 中执行批处理文件时,未映射驱动器?
Process process = new Process();
process.StartInfo.FileName = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\MAP.BAT";
process.StartInfo.CreateNoWindow = false;
process.Start();
How does one map network drive from windows service?
如何从windows服务中映射网络驱动?
采纳答案by Ahmed Atia
All issues solved by using Map Network Drive (API)to map network drive. I map required drives while OnStart event of service.
使用映射网络驱动器(API)映射网络驱动器解决了所有问题。我在服务的 OnStart 时映射所需的驱动器。
回答by Andomar
I think services can't map network drives unless they have the right "interact with desktop" ?
我认为服务无映射网络驱动器,除非它们具有正确的“与桌面交互”?
Try to run the service under the "Network Service" account, instead of "Local System" ?
尝试在“网络服务”帐户下运行服务,而不是“本地系统”?
回答by MSalters
Are you running the service under the user account that belongs to the password? The MAP USEcommand will use the current user, unless you pass /USER:anotheruser
您是否在属于密码的用户帐户下运行该服务?该MAP USE命令将使用当前用户,除非您通过 /USER:anotheruser
回答by MSalters
It might be better to just call the right API function, instead of calling a batch file to call another executable. The function you're looking for is DefineDosDevice()
最好只调用正确的 API 函数,而不是调用批处理文件来调用另一个可执行文件。您正在寻找的功能是DefineDosDevice()
回答by Winston Smith
As far as I know, a mapped drive is only mapped for the duration of the user session. If your windows service is running on startup, or is otherwise not part of a user session, the drive mapping is lost.
据我所知,映射驱动器仅在用户会话期间映射。如果您的 Windows 服务在启动时运行,或者不是用户会话的一部分,则驱动器映射将丢失。
This may or may not be true but it is worth looking into - I remember something about it from a similar scenario I had about 7 years ago.
这可能是也可能不是,但值得研究——我记得我在大约 7 年前的一个类似场景中的一些事情。
回答by David L Morris
There are two issues:
有两个问题:
- Mappings are only in use for the user session, which means that effectively you can't use a mapped drive for a Service. You would need to use the uncpath.
- The second issue is that a service (using the local system account) does not have access to the network, or more specifically, to the resource required. To resolve this, you would need to, either: Give the 'computer' on which the service is running specific access to the folder, or, set up the service to use a network (DOMAIN) account that has access to the resource.
- 映射仅用于用户会话,这意味着实际上您不能将映射驱动器用于服务。您将需要使用UNC路径。
- 第二个问题是服务(使用本地系统帐户)无访问网络,或者更具体地说,无访问所需的资源。要解决此问题,您需要: 为运行该服务的“计算机”授予对该文件夹的特定访问权限,或者将该服务设置为使用有权访问该资源的网络 (DOMAIN) 帐户。
回答by Vimal Raj
This can be achieved using a reference to Windows Script Host Object Model.
这可以使用对 Windows 脚本宿主对象模型的引用来实现。
Add a COM reference to Windows Script Host Object Modelwhich adds an entry IWshRuntimeLibrary to the references section. Now you can use the following code to map a drive.
添加对Windows 脚本宿主对象模型的 COM 引用,这会将条目 IWshRuntimeLibrary 添加到引用部分。现在您可以使用以下代码来映射驱动器。
using IWshRuntimeLibrary;
IWshNetwork_Class network = new IWshNetwork_Class();
network.MapNetworkDrive("k:",@"\192.168.20.35\MyShare", Type.Missing, "user1", "password1");
you can use the following code to UnMap or remove the mapping.
您可以使用以下代码取消映射或删除映射。
network.RemoveNetworkDrive("k:");
I have documented the detailed steps here
我在这里记录了详细的步骤

