【问题标题】:UWP Parse folder from absolute or relative pathUWP 从绝对或相对路径解析文件夹
【发布时间】:2017-05-23 13:39:25
【问题描述】:

我想解析一个用户可以选择的文件夹。 但如果我理解的话,UWP 中不允许使用绝对路径,因为媒体(xbox、windows phone、windows 桌面等)之后的磁盘不一样?

所以,我有一个名为 Parser 的类,它可以解析用户选择的路径,但现在只能解析当前文件夹。

这不起作用:

Parser parser = new Parser(@"C:\a\b\c");
parser.createTreeView(tree);

请帮帮我。提前谢谢你。

编辑:这是我的解析器 class=>

public TreeViewItem Parse(DirectoryInfo directoryInfo)
    {
        try
        {
            var directoryNode = new TreeViewItem { Header = directoryInfo.Name };
            Convention convention = new Convention();

            foreach (var directory in directoryInfo.GetDirectories())
            {

                directoryNode.Items.Add(Parse(directory));
                System.Diagnostics.Debug.WriteLine("test : " + directory.Name);
            }
            foreach (var file in directoryInfo.GetFiles())
            {
                if (file.Name.Contains(EConvention.INSTALL))
                {
                    listFiles.Add(file.FullName);
                }

                TreeViewItem item = new TreeViewItem
                {
                    Header = Path.GetFileNameWithoutExtension(file.FullName),
                    Tag = file.FullName

                };
                directoryNode.Items.Add(item);
            }
            return directoryNode;
        }
        catch (System.UnauthorizedAccessException e)
        {
            //MessageDialog dialog = new MessageDialog(""+e.Message);
            dialogAsync(e.Message);
            return new TreeViewItem();
        }

    }


public void CreateTreeView(TreeView tree)
    {

        DirectoryInfo dir = new DirectoryInfo(pathToParse);
        System.Diagnostics.Debug.WriteLine("dir exists ? "+dir.Exists);
        if (dir.Exists)
        {
            System.Diagnostics.Debug.WriteLine("dir existe");
            TreeViewItem root = new TreeViewItem() { Header = dir.Name };
            root.Tag = dir;
            tree.Items.Add(Parse(dir));
        }
    }

【问题讨论】:

  • “解析文件夹”是什么意思?向我们展示Parser 类的完整代码也会有所帮助
  • 我想解析一个文件夹来创建一个TreeView。我编辑我的问题,向您展示我的 Parser 课程。
  • 那么它如何以及以何种方式不解析任何其他文件夹?
  • 当我将另一个路径放入文件夹时,我的条件:if(dir.Exists) 返回 false

标签: c# path uwp


【解决方案1】:

UWP 应用无权访问设备上的所有文件。默认情况下,应用程序可以访问某些文件系统位置。应用程序还可以通过文件选择器或通过声明功能来访问其他位置。更多信息请查看File access permissions

虽然我们可以在 UWP 应用中使用DirectoryInfo,但它只能使用默认情况下 UWP 应用可以访问的文件夹,例如安装目录和本地文件夹等。System.IO namespaces for UWP apps 中的大多数类型都有类似的局限性。在 UWP 中处理文件或文件夹时,一个重要的规则是Skip the path: stick to the StorageFile

您可以使用Folder​Picker 让用户选择一个文件夹,然后将其添加到您应用的FutureAccessListMostRecentlyUsedList 以跟踪它。您可以在How to track recently-used files and folders 中了解有关使用这些列表的更多信息。在此之后,您将能够随时从FutureAccessListMostRecentlyUsedList 检索StorageFolder

获得StorageFolder 后,您可以在Parse 中使用GetFilesAsync()GetFoldersAsync() 方法,而不是DirectoryInfo.GetDirectoriesDirectoryInfo.GetFiles 方法。

【讨论】:

    猜你喜欢
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 2010-10-03
    • 2020-11-03
    • 1970-01-01
    相关资源
    最近更新 更多