2013-07-11 44 views
2
var di = new DirectoryInfo(Root); 
var diList = di.GetDirectories(); 

上面的內容在訪問位於網絡上的文本路徑「Root」時崩潰。我只在用戶首次登錄到Windows 7時纔會發生(未在其他操作系統上測試過)。如果用戶使用Windows資源管理器導航到指定的路徑,則di能夠檢索目錄。var di = new DirectoryInfo(路徑)拋出異常,直到我使用Windows資源管理器第一次打開路徑

我知道做一個捕獲,並提示用戶點擊一個鏈接,打開Windows資源管理器和導航到路徑可以完成,但我不是一個解決辦法。我希望我的程序能夠建立到網絡路徑的連接,而不需要Windows資源管理器首先執行它。

例外:

System.IO.IOException: The network name cannot be found. 
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 
at System.IO.FileSystemEnumerableIterator`1.CommonInit() 
at System.IO.FileSystemEnumerableIterator`1..ctor(String path,string originalUserPath, 
    String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler, 
    Boolean checkHost) 
at System.IO.DirectoryInfo.InternalGetDirectories(String searchPattern, 
SearchOption searchOption) at Controls.ValidationControl.LoadDBox(Object sender, 
RoutedEventArgs e) 
in c:\Controls\ValidationControl.xaml.cs:line 1010 

預先感謝您的支持, 約翰

+0

你能分享當它「崩潰」時發生的實際異常嗎? – Scottie

+0

{System.IO.IOException:無法找到網絡名稱。 在System.IO .__ Error.WinIOError(的Int32的errorCode,字符串maybeFullPath) 在System.IO.FileSystemEnumerableIterator'1.CommonInit() 在System.IO.FileSystemEnumerableIterator'1..ctor(字符串路徑,串originalUserPath,字符串是searchPattern ,SearchOption searchOption,SearchResultHandler'1 resultHandler,Boolean checkHost)在System.IO.DirectoryInfo.InternalGetDirectories(String searchPattern,SearchOption searchOption)上的Controls.ValidationControl.LoadDBox(對象發件人,RoutedEventArgs e)位於c:\ Controls \ ValidationControl.xaml中。cs:1010行 – Cyphryx

+0

注意:private const string Root = @「\\ networkPath」; – Cyphryx

回答

2

Microsoft的安全性將阻止您通過UNC使用應用程序捕獲的憑據訪問SharePoint。因此,嘗試所有上述解決方案和其他3分網絡接入類後,沒有人會看到的路徑,讓我登錄所以我只是用

Process.Start(dir); 

,並提示用戶來是怎麼回事。 Windows資源管理器啓動,使用用戶的憑據並導航到Windows然後使用用戶的域帳戶登錄到SharePoint的路徑。 Windows可以做到這一點。不適用於微軟的程序員不能。

1

既然你說,它的發生在網絡文件夾,我的第一個傾向是,這個應用程序正在運行的Windows進程在沒有安全訪問權限的情況下閱讀目錄信息。當用戶瀏覽到此文件夾時,安全性已通過身份驗證,因此您的應用現在可以讀取它。

如果Root是本地文件夾,應用程序是否工作?

+0

你是對的。需要Windows身份驗證才能訪問資源,直到用戶使用資源管理器導航到網絡路徑之後纔會授予該資源。我如何打電話給Windows使用C#手動從我的代碼訪問權限? – Cyphryx

+0

是的,本地文件夾工作得很好。 – Cyphryx

1

最可能的原因似乎是用戶需要登錄才能訪問網絡路徑。直到通過Explorer訪問路徑纔會發生這種情況。嘗試將網絡文件夾映射爲本地計算機上的驅動器,並將其設置爲自動登錄。這可能有幫助。

編輯: - 正如Scottie在另一個答案中指出的程序運行的憑據可能還需要訪問共享文件夾。

編輯2: -您可以使用P/Invoke通過憑證 [Ref]。或者,您可以撥打LogonUser方法進行冒充,如here所述。有關假冒和LogonUser的更多詳細信息,請參閱this question and its answers

+0

因此,在C#中,如何使用現有用戶的登錄來訪問網絡路徑? var user = WindowsIdentity.GetCurrent(); ..... – Cyphryx

+0

我嘗試了上述兩個都不會授予我訪問權限。不過謝謝你的幫忙。 – Cyphryx

相關問題