UWP GetFilesAsync

时间:2017-11-09 18:02:42

标签: c# uwp

我试图获得所有" *。csv" LocalState文件夹中的文件。并得到以下错误。

System.ArgumentException:'参数不正确。

这是我的代码:

        StorageFolder appInstalledFolder = ApplicationData.Current.LocalFolder;
        StorageFolder assets = await appInstalledFolder.GetFolderAsync("*.csv");
        var files = await assets.GetFilesAsync();

2 个答案:

答案 0 :(得分:3)

您可以使用文件查询来实现此目的:

//this may be any folder you want.
StorageFolder folder = ApplicationData.Current.LocalFolder;
var options = new QueryOptions();
options.FileTypeFilter.Add(".csv");//this will add .csv files to query options 
options.FolderDepth = FolderDepth.Deep;//optional
StorageFileQueryResult query = folder.CreateFileQueryWithOptions(options);
IReadOnlyList<StorageFile> fileList = await query.GetFilesAsync();

MSDN - StorageFileQueryResult

希望这会有所帮助..

答案 1 :(得分:0)

以下代码找到了具有* .csv属性的文件。

    IReadOnlyList<StorageFile> x = await ApplicationData.Current.LocalFolder.GetFilesAsync();
        int tempCt = x.Count;
        foreach (StorageFile file in x)
        {

            if (file.Name.ToString().Contains("csv"))
            {
                sb.Append(file.Name + System.Environment.NewLine);
            }
        }

可能不是最好的,但它确实有效。