如何从Windows服务映射网络驱动器?
我正在尝试从 Windows 服务映射网络驱动器,我使用批处理文件执行以下命令
NET USE U: \\192.168.55.6\folder password
在服务构造函数或 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();
如何从Windows服务映射网络驱动器?
I'm trying to map network drive from windows service, I use batch file for executing the following command
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?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这可以通过使用 Windows 脚本宿主对象模型的引用来实现。
添加对Windows 脚本宿主对象模型的 COM 引用,这会将条目 IWshRuntimeLibrary 添加到引用部分。 现在您可以使用以下代码来映射驱动器。
您可以使用以下代码来取消映射或删除映射。
我已记录详细步骤
This can be achieved using a reference to Windows Script Host Object Model.
Add a COM reference to Windows Script Host Object Model which adds an entry IWshRuntimeLibrary to the references section. Now you can use the following code to map a drive.
you can use the following code to UnMap or remove the mapping.
I have documented the detailed steps here
通过使用映射网络驱动器 (API) 映射网络驱动器解决了所有问题。 我在服务 OnStart 事件时映射所需的驱动器。
All issues solved by using Map Network Drive (API) to map network drive. I map required drives while OnStart event of service.
最好只调用正确的 API 函数,而不是调用批处理文件来调用另一个可执行文件。 您要查找的函数是
DefineDosDevice ()
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()
有两个问题:
无法访问网络,或更具体地说,无法访问
所需资源。 要解决此问题,您需要:
运行服务的“计算机”特定访问
文件夹,或者将服务设置为使用网络 (域) 帐户
有权访问该资源。
There are two issues:
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.
您是否在属于该密码的用户帐户下运行该服务?
MAP USE
命令将使用当前用户,除非您传递 /USER:anotheruserAre you running the service under the user account that belongs to the password? The
MAP USE
command will use the current user, unless you pass /USER:anotheruser我认为服务无法映射网络驱动器,除非它们具有正确的“与桌面交互”?
尝试在“网络服务”帐户而不是“本地系统”帐户下运行该服务?
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" ?
据我所知,映射驱动器仅在用户会话期间进行映射。 如果您的 Windows 服务在启动时运行,或者不是用户会话的一部分,则驱动器映射将会丢失。
这可能是真的,也可能不是真的,但值得研究一下——我记得大约 7 年前的一个类似场景。
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.
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.