简体   繁体   English

var di = new DirectoryInfo(path)引发异常,直到我第一次使用Windows资源管理器打开路径

[英]var di = new DirectoryInfo(path) throws exception until I use Windows Explorer to open the path for a first time

var di = new DirectoryInfo(Root);
var diList = di.GetDirectories();

The above crashes when accessing the text path 'Root' that is located on the network. 当访问网络上的文本路径“ Root”时,以上内容崩溃。 I only occurs when a user first logs into Windows 7 (not tested on other OS's). 仅当用户首次登录Windows 7(未经其他操作系统测试)时,我才会发生。 If a user uses windows Explorer to navigate to the specified path, then di is able to retrieve the directories. 如果用户使用Windows资源管理器导航到指定的路径,则di可以检索目录。

I know doing a catch and prompting the user to click a link that opens windows explorer and navigates to the path can be done but I'm not one for work arounds. 我知道可以进行捕获并提示用户单击打开Windows资源管理器并导航到路径的链接,但是我不是解决问题的人。 I want my program to be able to establish the connection to the network path without requiring Windows explorer to do it first. 我希望我的程序能够建立与网络路径的连接,而无需Windows Explorer首先进行连接。

The exception: 例外:

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

Thank you in advance for your support, John 约翰,谢谢您的支持。

Microsoft's security will prevent you from accessing SharePoint via UNC using credentials captured by an application. Microsoft的安全性将阻止您使用应用程序捕获的凭据通过UNC访问SharePoint。 Therefore, after trying all above solutions and 3 other Network access classes, none would see the path and let me login so I simply used 因此,在尝试了所有上述解决方案和其他3个网络访问类之后,没有人会看到该路径并让我登录,所以我只用了

Process.Start(dir);

and prompt the user as to what is going on. 并提示用户正在发生的事情。 Windows explorer launches, using the user's credentials and navigates to the path where Windows then uses the user's domain account to login to SharePoint. Windows资源管理器将使用用户的凭据启动,并导航到Windows然后使用用户的域帐户登录SharePoint的路径。 Windows can do it. Windows可以做到。 Programmers that don't work for Microsoft can't. 不适用于Microsoft的程序员不能。

Since you said it's happening on network folders, my first inclination is that the windows process that this app is running under doesn't have security access to read the directory info. 既然您说过这是在网络文件夹上发生的,那么我的第一个倾向是,此应用程序在其下运行的Windows进程没有安全性访问权限来读取目录信息。 When the user browses to this folder, the security is authenticated, and so your app can now read it. 当用户浏览到该文件夹​​时,安全性便得到了验证,因此您的应用程序现在可以读取它。

Does the app work if Root is a local folder? 如果Root是本地文件夹,则该应用程序可以正常工作吗?

Most probable reason seems that user need to log in to access the network path. 最可能的原因似乎是用户需要登录才能访问网络路径。 This does not seems to happen until the path is accessed via Explorer . 在通过Explorer访问路径之前,似乎不会发生这种情况。 Try mapping the network folder as drive on your local machine and set it to login automatically. 尝试将网络文件夹映射为本地计算机上的驱动器,并将其设置为自动登录。 This might help it. 这可能会有所帮助。

EDIT:- As pointed by Scottie in another answer the credentials under which the program runs may also need access to the shared folder. 编辑:-正如Scottie在另一个答案中指出的那样,程序在其下运行的凭据也可能需要访问共享文件夹。

EDIT#2:- You can use P/Invoke to pass Credentials [Ref] . EDIT#2:-您可以使用P/Invoke传递凭据[Ref] Or you can call LogonUser method to do impersonation as explained here . 或者,你可以调用LogonUser方法做模拟为解释在这里 Further details regarding impersonation and LogonUser can be found in this question and its answers . 有关模拟和LogonUser更多详细信息,可以在此问题及其答案中找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DirectoryInfo为正确的路径抛出“参数异常:路径不是合法形式” - DirectoryInfo throws “Argument Exception: The path is not of a legal form” for the correct path 如果 Windows 资源管理器在特定路径打开,则不要创建新实例 - If Windows explorer is open at a specific path, do not create a new instance 在所有Open Explorer Windows的ListBox中获取路径 - Get Path in a ListBox of all Open Explorer Windows 获取directoryinfo对象的路径 - Get path of directoryinfo object PDFClown .NET打开Windows资源管理器,而不指定路径 - PDFClown .NET Open a Windows Explorer instead of specifying path 如何使用RichTextBox使用获取目录路径db打开Window Explorer? - How do I use RichTextBox to open Window Explorer with get directory path, db? DirectoryInfo,FileInfo和很长的路径 - DirectoryInfo, FileInfo and very long path 在Windows资源管理器中打开文件夹结构时,DirectoryInfo.Delete(True)不会删除 - DirectoryInfo.Delete(True) Doesn't Delete When Folder Structure is Open in Windows Explorer 如何向用户询问路径,然后为Directoryinfo放置与目录相同的路径? - How can I Ask the user for a Path and then put the same Path as a Directory for a Directoryinfo? DirectoryInfo仅从localhost查找路径 - DirectoryInfo only finds path from localhost
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM