用户需要在启动我们的应用程序时使用他的Windows凭据登录。这些凭据用于模拟用户并在提供的登录名下运行主表单。我们现在有一个OpenFileDialog
,用户可以在其中选择文件。
当用户访问映射的网络驱动器时会出现问题(从登录到计算机的用户而不是我的程序中显示的那些驱动器显示)。按下OK按钮时,OpenFileDialog
显示错误消息(无法找到/访问路径。确保它存在)。
正如我在其他帖子中看到的那样,可以将这些路径映射回UNC路径,但对话框甚至没有返回,所以我可以这样做。除了制作我自己的打开文件对话框之外,还有一些解决方法吗?
假冒部分:
bool success = NativeMethods.LogonUser(userName, domain, password, (int)LogonType.Logon32LogonNewCredentials, (int)LogonProvider.Logon32ProviderWinnt50, ref pExistingTokenHandle);
if (success)
{
success = NativeMethods.DuplicateToken(pExistingTokenHandle, (int)SecurityImpersonationLevel.SecurityImpersonation, ref pDuplicateTokenHandle);
if (success)
{
// Return the impersonation context
WindowsIdentity identity = new WindowsIdentity(pDuplicateTokenHandle);
impersonationContext = identity.Impersonate();
return impersonationContext;
}
}
打开对话框部分
OpenFileDialog openFileDialog = new OpenFileDialog
{
Multiselect = true,
InitialDirectory = Environment.CurrentDirectory,
Title = "Select file"
};
bool? dialogResult = openFileDialog.ShowDialog(this);
if (dialogResult.Value)
{
openFileDialog.FileNames.ToList().ForEach(t => MessageBox.Show("File: " + t));
}
答案 0 :(得分:0)
在显示对话框之前撤消模拟已解决了在网络驱动器上选择文件的问题。问题本身仍然有效,因为服务帐户可能还需要访问网络驱动器。