简体   繁体   中英

Deleting files created via CreateFileAsync?

*Microsoft on UWP apps - creating, writing, and reading

My C# project creates jpegs dynamically using the CreateFileAsync() method, but I want to be able to delete the files I create during runtime as well and the only delete methods I could find were for deleting folders, not specific files. How do I go about this?

In UWP app, if we need to delete a file, firstly we should get the file, then use the DeleteAsync() method to delete it.

For example, if you placed the files under the ApplicationData.Current.LocalFolder, using the following code to delete temp1.jpg.

private async void delete_click(object sender, RoutedEventArgs e)
{
        StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("temp1.jpg");
        if (filed != null)
        {
            await filed.DeleteAsync();
        }
}

Another way is to centralize the file path under one folder, simply we can delete all files under it instead of retriving and deleting files one by one.

You can use:

var filename = "fileYouWantToDelete";
using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
     isf.DeleteFile(fileName);

All you need to know where the file is placed;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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