简体   繁体   English

winrt如何使用C#从设备获取文件列表

[英]winrt how to get files list from device using c#

I need get a list of all files in device (phone or PC) in my universal app. 我需要在通用应用程序中获取设备(电话或PC)中所有文件的列表。 In wpf I did somesing like that: 在WPF中,我做了这样的事情:

class Collection {
    private StringCollection seachResults; 
    //find all mp3 files in local storage
    private void ScanDrives() {
        seachResults.Clear();
        string[] drives = Environment.GetLogicalDrives();
        foreach (string dr in drives) {
            DriveInfo di = new DriveInfo(dr);
            if (!di.IsReady) {
                //skip if drive not ready
                continue;
            }
            DirectoryInfo rootDir = di.RootDirectory;
            WalkDirectoryTree(rootDir);
        }
    }

    private void WalkDirectoryTree(DirectoryInfo root) {
        FileInfo[] files = null;
        DirectoryInfo[] subDirs = null;
        try {
            files = root.GetFiles("*.mp3");
        } catch (UnauthorizedAccessException e) {

        } catch (DirectoryNotFoundException e) {

        }

        if (files != null) {
            foreach (FileInfo fileInfo in files) {
                seachResults.Add(fileInfo.FullName);
            }
            subDirs = root.GetDirectories();
            foreach (DirectoryInfo dirInfo in subDirs) {
                WalkDirectoryTree(dirInfo);
            }
        }
    }
}

But when I try to migrate this into winRT app I get a few errors like unknown type Drive and unexisted method Environment.GetLogicalDrives() . 但是,当我尝试将其迁移到winRT应用程序时,遇到一些错误,例如unknown type Drive和不存在的unexisted method Environment.GetLogicalDrives()

Can anyone say how do that in winRT? 谁能说出在winRT中如何做?

You won't find a method for getting all logical drives in a WinRT app; 您找不到在WinRT应用程序中获取所有逻辑驱动器的方法。 WinRT apps exist in a sandboxed environment and will only have access to their own isolated storage or known folders (such as music) if declared as a capability in the application manifest. WinRT应用程序存在于沙盒环境中,并且只有在应用程序清单中声明为功能时,才可以访问其自己的隔离存储或已知文件夹(例如音乐)。

For example, to get access to the user's music folder you can do this (don't forget to declare the capability in the app manifest): 例如,要访问用户的音乐文件夹,您可以执行以下操作(不要忘记在应用清单中声明该功能):

StorageFolder folder = Windows.Storage.KnownFolders.MusicLibrary;

The only way to get access to any other part of the file system is if the user specifically grants access via a file picker: 访问文件系统任何其他部分的唯一方法是用户特别通过文件选择器授予访问权限:

var folderPicker = new FolderPicker();
var folder = await folderPicker.PickSingleFolderAsync();

Have you tried System.IO.Directory.GetLogicalDrives() ? 您是否尝试过System.IO.Directory.GetLogicalDrives()

I believe that Environment.GetLogicalDrives() only works for Win32/Win64. 我相信Environment.GetLogicalDrives()仅适用于Win32 / Win64。 If I am not mistaken System.IO.Directory exists in mscorlib, and is widely available across Phone, RT, or Regular versions. 如果我没有记错的话,System.IO.Directory存在于mscorlib中,并且可以在Phone,RT或Regular版本中广泛使用。

The MSDN reference: MSDN参考:

https://msdn.microsoft.com/en-us/library/system.io.directory.getlogicaldrives%28v=vs.110%29.aspx https://msdn.microsoft.com/zh-cn/library/system.io.directory.getologicdrives%28v=vs.110%29.aspx

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM