本文讲述:在win10系统,网络盘读取问题。
在WIN10系统,写了一个程序,读取系统文件夹,让用户选择目录,代码如下:
System.Windows.Forms.FolderBrowserDialog folder = new System.Windows.Forms.FolderBrowserDialog();
if (folder.ShowDialog() == DialogResult.OK)
{
}
然而在调试过程中发现:Win7系统,网络盘,可以正常识别;然而在Win10系统中,网络盘,识别不出来。
原因:经过调试和网上搜索发现:Win10系统中,以管理员身份运行就不能读网络盘,不以管理员身份运行程序就能读网络盘;能读网络盘,又不能以管理员身份运行。程序不以管理员身份运行,有些地方操作就没有权限,比如创建文件夹。
解决方案:在Win10系统,无论管理员身份还是非管理员身份,都能在打开文件对话框看到网络映射盘,需要更改计算机注册表。
示例代码如下:
//获得当前登录的Windows用户标示
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
//判断当前登录用户是否为管理员
if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))//以管理员身份运行,try{}catch(Except ex){}
{
RegistryKey key = Registry.LocalMachine;
RegistryKey system = key.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System", true);
if (system == null)
system = key.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System");
system.SetValue("EnableLinkedConnections", 1, RegistryValueKind.DWord);
}
执行上诉代码在注册表中添加一个DWORD值后,打开程序调用文件夹选择对话框,就能正常显示网络盘了。