如何从Windows服务映射网络驱动器?

发布于 2024-07-18 20:10:07 字数 476 浏览 11 评论 0原文

我正在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

爱,才寂寞 2024-07-25 20:10:07

这可以通过使用 Windows 脚本宿主对象模型的引用来实现。

添加对Windows 脚本宿主对象模型的 COM 引用,这会将条目 IWshRuntimeLibrary 添加到引用部分。 现在您可以使用以下代码来映射驱动器。

using IWshRuntimeLibrary;

IWshNetwork_Class network = new IWshNetwork_Class();
network.MapNetworkDrive("k:",@"\\192.168.20.35\MyShare", Type.Missing, "user1", "password1");

您可以使用以下代码来取消映射或删除映射。

network.RemoveNetworkDrive("k:");

我已记录详细步骤

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.

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

魂ガ小子 2024-07-25 20:10:07

通过使用映射网络驱动器 (API) 映射网络驱动器解决了所有问题。 我在服务 OnStart 事件时映射所需的驱动器。

All issues solved by using Map Network Drive (API) to map network drive. I map required drives while OnStart event of service.

我不在是我 2024-07-25 20:10:07

最好只调用正确的 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()

御弟哥哥 2024-07-25 20:10:07

有两个问题:

  1. 映射仅用于用户会话,这意味着您实际上无法将映射驱动器用于服务。 您需要使用 路径。
  2. 第二个问题是一个服务(使用本地系统帐户)
    无法访问网络,或更具体地说,无法访问
    所需资源。 要解决此问题,您需要:
    运行服务的“计算机”特定访问
    文件夹,或者将服务设置为使用网络 (域) 帐户
    有权访问该资源。

There are two issues:

  1. 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 path.
  2. 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.
万水千山粽是情ミ 2024-07-25 20:10:07

您是否在属于该密码的用户帐户下运行该服务? MAP USE 命令将使用当前用户,除非您传递 /USER:anotheruser

Are 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

人│生佛魔见 2024-07-25 20:10:07

我认为服务无法映射网络驱动器,除非它们具有正确的“与桌面交互”?

尝试在“网络服务”帐户而不是“本地系统”帐户下运行该服务?

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" ?

怎言笑 2024-07-25 20:10:07

据我所知,映射驱动器仅在用户会话期间进行映射。 如果您的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文