2014-03-06 98 views
3

用户需要在我们的应用程序启动时用他的Windows凭据登录。这些凭据用于模拟用户并在提供的登录名下运行主窗体。我们现在有一个OpenFileDialog用户可以选择文件。假冒时使用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

已尝试运行您的应用程序与提高privledges? – Kcvin

+0

是的,但这不会给我驱动器映射 – Scoregraphic

回答

0

撤消模拟解决了网络驱动器上选择文件的问题。问题本身仍然有效,因为服务帐户可能需要访问网络驱动器。