简体   繁体   English

如何清空c#xaml metro风格的目录?

[英]how to empty a directory in c# xaml metro style?

i have this c# code and i want to delete a certain sub directory in Documents Library. 我有这个c#代码,我想删除文档库中的某个子目录。 However this produces an error because the directory is not empty. 但是,这会产生错误,因为该目录不为空。 I hope someone can guide me on how to do this. 我希望有人可以指导我如何做到这一点。

thank you for any prompt reply. 谢谢你的任何及时回复。

StorageFolder storageFolder = KnownFolders.DocumentsLibrary;              
var queryResult = storageFolder.CreateFolderQuery();
IReadOnlyList<StorageFolder> folderList = await queryResult.GetFoldersAsync();

 foreach (StorageFolder folder in folderList)
    {
       await folder.DeleteAsync();             
    }

You could use the StorageFolder.GetFilesAsync() to obtain a list of all the files present in the folders and delete them prior to deleting the folders since there is no way in the DeleteAsync() method to specify subfolders and files. 您可以使用StorageFolder.GetFilesAsync()获取文件夹中存在的所有文件的列表,并在删除文件夹之前将其删除,因为DeleteAsync()方法无法指定子文件夹和文件。

More info: StorageFolder class | 更多信息: StorageFolder类 | MSDN MSDN

Hope this may help. 希望这可能有所帮助。

 public async void deletefile()
        {
            StorageFolder sourceFolder =  ApplicationData.Current.TemporaryFolder;
           // sourceFolder = await sourceFolder.GetFolderAsync("Test");
           // await sourceFolder.DeleteAsync(StorageDeleteOption.PermanentDelete);


          // var files = await sourceFolder.GetFilesAsync();

           IReadOnlyList<StorageFile> folderList = await sourceFolder.GetFilesAsync();
            if (folderList.Count > 0)
            {
                foreach (StorageFile f1 in folderList)
                {

                    await f1.DeleteAsync(StorageDeleteOption.PermanentDelete);
                }
            }

           //StorageFile retfile = await ApplicationData.Current.TemporaryFolder.GetFileAsync("MysoundFile.mp3");
           // if (retfile != null)
           // {
           //     await retfile.DeleteAsync(StorageDeleteOption.PermanentDelete);
           // }


        }

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

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