我需要访问网络路径
我有一个 Windows 服务并尝试使用 pinvoke 来映射它。该代码在控制台应用程序中运行良好,但在 Windows 服务中不起作用。据此:
我应该放弃网络使用方法,而是给予适当的权限,以便我的服务可以访问 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以作为有权访问此路径的域/Windows 用户启动 Windows 服务。
You could launch your Windows service as a domain/windows user that does have access to this path.