[英]winrt how to get files list from device using c#
我需要在通用應用程序中獲取設備(電話或PC)中所有文件的列表。 在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);
}
}
}
}
但是,當我嘗試將其遷移到winRT應用程序時,遇到一些錯誤,例如unknown type Drive
和不存在的unexisted method Environment.GetLogicalDrives()
。
誰能說出在winRT中如何做?
您找不到在WinRT應用程序中獲取所有邏輯驅動器的方法。 WinRT應用程序存在於沙盒環境中,並且只有在應用程序清單中聲明為功能時,才可以訪問其自己的隔離存儲或已知文件夾(例如音樂)。
例如,要訪問用戶的音樂文件夾,您可以執行以下操作(不要忘記在應用清單中聲明該功能):
StorageFolder folder = Windows.Storage.KnownFolders.MusicLibrary;
訪問文件系統任何其他部分的唯一方法是用戶特別通過文件選擇器授予訪問權限:
var folderPicker = new FolderPicker();
var folder = await folderPicker.PickSingleFolderAsync();
您是否嘗試過System.IO.Directory.GetLogicalDrives()
?
我相信Environment.GetLogicalDrives()僅適用於Win32 / Win64。 如果我沒有記錯的話,System.IO.Directory存在於mscorlib中,並且可以在Phone,RT或Regular版本中廣泛使用。
MSDN參考:
https://msdn.microsoft.com/zh-cn/library/system.io.directory.getologicdrives%28v=vs.110%29.aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.