我的程序需要读取和写入可能位于其他域中的其他计算机上的文件夹。所以我使用System.Runtime.InteropServices添加共享文件夹。当它在我的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;
}
private void AddSharedFolder(string name, string domain, string username, string password)
{
if (name == null || domain == null || username == null || password == null)
return;
USE_INFO_2 useInfo = new USE_INFO_2();
useInfo.ui2_remote = name;
useInfo.ui2_password = password;
useInfo.ui2_asg_type = 0; //disk drive
useInfo.ui2_usecount = 1;
useInfo.ui2_username = username;
useInfo.ui2_domainname = domain;
uint paramErrorIndex;
uint returnCode = NetUseAdd(String.Empty, 2, ref useInfo, out paramErrorIndex);
if (returnCode != 0)
{
throw new Win32Exception((int)returnCode);
}
}
答案 0 :(得分:1)
计算机中的每个线程都在特定的用户帐户下运行。共享文件夹具有安全设置,即它们受基于ACL的访问控制的约束,因此某些用户可能具有访问权限而其他用户可能没有访问权限。这意味着程序中的线程可能能够“看到”某些共享文件夹,而同一台计算机中的其他线程(包括使用桌面的交互式用户)可能无法“看到”这些文件夹。
总结:你应该假设很少。