2014-03-06 99 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

撤消模擬解決了網絡驅動器上選擇文件的問題。問題本身仍然有效,因爲服務帳戶可能需要訪問網絡驅動器。