我需要访问网络路径

发布于 2024-11-04 10:36:17 字数 1629 浏览 7 评论 0原文

我有一个 Windows 服务并尝试使用 pinvoke 来映射它。该代码在控制台应用程序中运行良好,但在 Windows 服务中不起作用。据此:

Windows 服务中的“net use”命令

我应该放弃网络使用方法,而是给予适当的权限,以便我的服务可以访问 UNC 路径( \server\share\file-path )。我如何设置这些权限?我认为应该在某处设置基本的 Windows 权限。任何帮助表示赞赏。

[DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern System.UInt32 NetUseAdd(string UncServerName, int Level, ref USE_INFO_2 Buf, out uint ParmError);

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct USE_INFO_2
{
    internal LPWSTR ui2_local;
    internal LPWSTR ui2_remote;
    internal LPWSTR ui2_password;
    internal DWORD ui2_status;
    internal DWORD ui2_asg_type;
    internal DWORD ui2_refcount;
    internal DWORD ui2_usecount;
    internal LPWSTR ui2_username;
    internal LPWSTR ui2_domainname;
}

和...

       static void Main()
        {
            USE_INFO_2 useInfo = new USE_INFO_2();
            useInfo.ui2_remote = @"\\xx.xx.xx.xx\E$"; // "\\xx.xx.xx.xx\E$"
            useInfo.ui2_password = "********";
            useInfo.ui2_asg_type = 0;    //disk drive
            useInfo.ui2_usecount = 1;
            useInfo.ui2_username = "Admin";
            useInfo.ui2_domainname = "rendering";
            uint paramErrorIndex;
            uint returnCode = NetUseAdd(String.Empty, 2, ref useInfo, out paramErrorIndex);
            if (returnCode != 0)
            {
                throw new Win32Exception((int)returnCode);
            }
...

I have a windows service and tried to map it using the pinvoke. The code works fine in a console application, but it has no effect in a windows service. According to this:

"net use" command in a Windows Service

I should drop the net use approach and instead give the appropriate privileges, so that my service can access the UNC path ( \server\share\file-path ). How do I set up these privileges? I assume it's basic Windows privileges that should be set somewhere. Any help is appreciated.

[DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern System.UInt32 NetUseAdd(string UncServerName, int Level, ref USE_INFO_2 Buf, out uint ParmError);

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct USE_INFO_2
{
    internal LPWSTR ui2_local;
    internal LPWSTR ui2_remote;
    internal LPWSTR ui2_password;
    internal DWORD ui2_status;
    internal DWORD ui2_asg_type;
    internal DWORD ui2_refcount;
    internal DWORD ui2_usecount;
    internal LPWSTR ui2_username;
    internal LPWSTR ui2_domainname;
}

and...

       static void Main()
        {
            USE_INFO_2 useInfo = new USE_INFO_2();
            useInfo.ui2_remote = @"\\xx.xx.xx.xx\E$"; // "\\xx.xx.xx.xx\E$"
            useInfo.ui2_password = "********";
            useInfo.ui2_asg_type = 0;    //disk drive
            useInfo.ui2_usecount = 1;
            useInfo.ui2_username = "Admin";
            useInfo.ui2_domainname = "rendering";
            uint paramErrorIndex;
            uint returnCode = NetUseAdd(String.Empty, 2, ref useInfo, out paramErrorIndex);
            if (returnCode != 0)
            {
                throw new Win32Exception((int)returnCode);
            }
...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

阳光①夏 2024-11-11 10:36:17

您可以作为有权访问此路径的域/Windows 用户启动 Windows 服务。

You could launch your Windows service as a domain/windows user that does have access to this path.

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