"DeleteAsync" 和 "File.Delete" 之间的差异以及为什么 "DeleteAsync" 不删除文件?
Differences between "DeleteAsync" and "File.Delete" and why "DeleteAsync" doesn't remove file?
想知道为什么 "DeleteAsync" 不删除文件但 "File.Delete" 会删除文件。谁可以给我解释一下这个?起初我认为文件是打开的,但如果文件是打开的 "File.Delete" 也不应该删除它或者...?
private static async void FILESYSTEM_RemoveVideoPosterIfExist(string posterFileNameOnStorage)
{
IStorageItem videoPosterIStorageItem = await ApplicationData.Current.LocalFolder.TryGetItemAsync(SYSTEM_UserVideoPosterFolder + @"\" + DATABASE_SelectedUserInformation.UserName + "." + SYSTEM_UserPosterFolderExtension + @"\" + posterFileNameOnStorage);
if (videoPosterIStorageItem != null)
{
try
{
//Why this doesn't delete file...
await videoPosterIStorageItem.DeleteAsync(StorageDeleteOption.PermanentDelete);
}
catch
{
//But this one will delete file.
StorageFolder applicationStorageFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync(SYSTEM_UserVideoPosterFolder + @"\" + DATABASE_SelectedUserInformation.UserName + "." + SYSTEM_UserPosterFolderExtension + @"\");
File.Delete(applicationStorageFolder.Path + @"\" + posterFileNameOnStorage);
}
}
}
原因很可能是本地没有异步删除文件的功能。托管 API 通常是非托管 API 的包装器。
看看这个
Why isn't there an asynchronous file delete in .net?
FileInfo fi = new FileInfo(fileName);
await fi.DeleteAsync(); // C# 5
fi.DeleteAsync().Wait(); // C# 4
希望对您有所帮助!!
想知道为什么 "DeleteAsync" 不删除文件但 "File.Delete" 会删除文件。谁可以给我解释一下这个?起初我认为文件是打开的,但如果文件是打开的 "File.Delete" 也不应该删除它或者...?
private static async void FILESYSTEM_RemoveVideoPosterIfExist(string posterFileNameOnStorage)
{
IStorageItem videoPosterIStorageItem = await ApplicationData.Current.LocalFolder.TryGetItemAsync(SYSTEM_UserVideoPosterFolder + @"\" + DATABASE_SelectedUserInformation.UserName + "." + SYSTEM_UserPosterFolderExtension + @"\" + posterFileNameOnStorage);
if (videoPosterIStorageItem != null)
{
try
{
//Why this doesn't delete file...
await videoPosterIStorageItem.DeleteAsync(StorageDeleteOption.PermanentDelete);
}
catch
{
//But this one will delete file.
StorageFolder applicationStorageFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync(SYSTEM_UserVideoPosterFolder + @"\" + DATABASE_SelectedUserInformation.UserName + "." + SYSTEM_UserPosterFolderExtension + @"\");
File.Delete(applicationStorageFolder.Path + @"\" + posterFileNameOnStorage);
}
}
}
原因很可能是本地没有异步删除文件的功能。托管 API 通常是非托管 API 的包装器。
看看这个
Why isn't there an asynchronous file delete in .net?
FileInfo fi = new FileInfo(fileName);
await fi.DeleteAsync(); // C# 5
fi.DeleteAsync().Wait(); // C# 4
希望对您有所帮助!!