由于域文件夹重定向而在 Environment.SpecialFolder.Favorites 上调用 Directory.GetDirectories 时出现 DirectoryNotFoundException

发布于 2024-07-25 22:48:31 字数 1632 浏览 12 评论 0原文

我有一些 C# 代码尝试获取当前登录用户的收藏夹。 该代码是加载到 Windows 资源管理器进程中的任务栏工具栏的一部分。 我有一位用户正在使用 Windows Vista,并且在已设置并启用了漫游配置文件或文件夹重定向的域上启用了 UAC。 在收藏夹路径上调用 Directory.GetDirectories 时,会抛出“System.IO.DirectoryNotFoundException:找不到路径 'C:\Users\\Favorites\”的一部分。 其他域中没有漫游配置文件或文件夹重定向设置的其他用户不会遇到此问题。

用户还报告说,将失败日志中的路径复制到运行提示符中无法加载该路径,但如果他们直接使用资源管理器导航到该路径,然后将该路径复制并粘贴到运行提示符中,则可以正常工作。 他给我发了两条路径,它们完全相同,这根本没有任何意义。

我的理论是,这是由文件夹重定向引起的,其中该路径实际上指向服务器上的共享,但在尝试访问子目录(从 Directory.GetDirectories 返回的目录信息)时重定向失败。 初始目录可以工作,但初始目录的所有子目录无法正确重定向。

有人遇到过这样的情况和/或知道解决方法来正确访问重定向的文件夹吗?

private void GetFavorites()
    {
        try
        {
            System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Favorites));
            AddFavorites(dirInfo);
        }
        catch
        {
        }
    }

private void AddFavorites(DirectoryInfo dirInfo)
    {
        foreach (System.IO.FileInfo fileInfo in dirInfo.GetFiles("*.url"))
        {
            //string alias = fileInfo.Name.Replace(".url", "");

            if (!ItemsBookmarks.ContainsKey(fileInfo.Name))
                ItemsBookmarks.Add(fileInfo.Name, fileInfo.Name);

        }

        foreach (System.IO.FileInfo fileInfo in dirInfo.GetFiles("*.lnk"))
        {

            if (!ItemsBookmarks.ContainsKey(fileInfo.Name))
                ItemsBookmarks.Add(fileInfo.Name, fileInfo.Name);

        }

        foreach (System.IO.DirectoryInfo objDir in dirInfo.GetDirectories())
        {
            AddFavorites(objDir);
        }
    }

谢谢,

约翰

I have some C# code that tries to get the Favorites for the currently logged in user. The code is part of a Taskbar Toolbar that gets loaded into the Windows Explorer process. I have a user who is using Windows Vista with UAC enabled on a domain that either has Roaming Profiles or Folder Redirection setup and enabled. When calling Directory.GetDirectories on the Favorites path, it throws "System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Users\\Favorites\". Other users on other domains that do not have Roaming Profiles or Folder Redirection setup do not have this issue.

The user also reported that copying the path from the failed logs into the run prompt fails to load the path, but if they navigate to the path directly using explorer and then copy and paste that path into the run prompt, it works. He sent me both paths and they are exactly identical which doesn't make any sense at all.

My theory is that this is caused by the Folder Redirection where that path is actually pointing to a share on the server but the redirection is failing when trying to access the subdirectories (of the directoryInfo returned from Directory.GetDirectories). The initial directory works but all subdirectories of the initial directory fail to redirect correctly.

Has anyone come across a situation like this and/or know a workaround to gain proper access to redirected folders?

private void GetFavorites()
    {
        try
        {
            System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Favorites));
            AddFavorites(dirInfo);
        }
        catch
        {
        }
    }

private void AddFavorites(DirectoryInfo dirInfo)
    {
        foreach (System.IO.FileInfo fileInfo in dirInfo.GetFiles("*.url"))
        {
            //string alias = fileInfo.Name.Replace(".url", "");

            if (!ItemsBookmarks.ContainsKey(fileInfo.Name))
                ItemsBookmarks.Add(fileInfo.Name, fileInfo.Name);

        }

        foreach (System.IO.FileInfo fileInfo in dirInfo.GetFiles("*.lnk"))
        {

            if (!ItemsBookmarks.ContainsKey(fileInfo.Name))
                ItemsBookmarks.Add(fileInfo.Name, fileInfo.Name);

        }

        foreach (System.IO.DirectoryInfo objDir in dirInfo.GetDirectories())
        {
            AddFavorites(objDir);
        }
    }

Thanks,

John

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美羊羊 2024-08-01 22:48:31

我相信您遇到的问题与重新分析点有关。

请参阅:http://msdn.microsoft.com/en-us/library/ bb513869.aspx

请参阅:在 .net (c#) 中检查重分析点的最佳方法是什么?

使用以下语法可以避免该问题:

private void AddFavorites(string dirPath)
{
    try
        {
            foreach (string fileName in Directory.GetFiles(dirPath, "*.*", SearchOption.TopDirectoryOnly))
            {
                //string alias = fileInfo.Name.Replace(".url", "");

                if (!ItemsBookmarks.ContainsKey(fileInfo.Name))
                {
                    ItemsBookmarks.Add(fileName);
                }
            }

            foreach (string subDirName in Directory.GetDirectories(dirPath, "*.*", SearchOption.TopDirectoryOnly))
            {
                AddFavorites(objDir);
            }
        }
        catch
        {
            //error getting files or subdirs... permissions issue?
            //throw
        }
}

I believe the problem you are experiencing is related to Reparse Points.

See: http://msdn.microsoft.com/en-us/library/bb513869.aspx

See: What is the best way to check for reparse point in .net (c#)?

The problem can be avoided by using the following syntax:

private void AddFavorites(string dirPath)
{
    try
        {
            foreach (string fileName in Directory.GetFiles(dirPath, "*.*", SearchOption.TopDirectoryOnly))
            {
                //string alias = fileInfo.Name.Replace(".url", "");

                if (!ItemsBookmarks.ContainsKey(fileInfo.Name))
                {
                    ItemsBookmarks.Add(fileName);
                }
            }

            foreach (string subDirName in Directory.GetDirectories(dirPath, "*.*", SearchOption.TopDirectoryOnly))
            {
                AddFavorites(objDir);
            }
        }
        catch
        {
            //error getting files or subdirs... permissions issue?
            //throw
        }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文