[英]Why can't I delete files when my app is suspending?
我已经隔离了以下代码,并且它在OnNavigatedTo事件中有效,因此我知道该代码有效。 但是,我不能在那里使用它。 我需要在Suspending事件中使用它。 但这在那儿行不通。 而且,当我设置断点时,在此事件内的任何地方都不会遇到断点。 也没有编译时或运行时错误。
到底是怎么回事?
async void App_Suspending(
Object sender,
Windows.ApplicationModel.SuspendingEventArgs e)
{
IReadOnlyList<StorageFile> thefiles;
var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longtime");
thefiles = await localFolder.GetFilesAsync();
foreach (var f in thefiles)
{
await f.DeleteAsync(StorageDeleteOption.Default);
}
}
我的猜测是,当您使用这种方法等待时,应用程序退出Suspending方法,并以这种方式授予OS杀死进程的权限。 您可以通过在第一次等待之后(在foreach上)放置一个断点并检查是否达到断点来进行测试。
我找到了解决方案。 它涉及检查应用程序是否已被用户关闭。 如果是这样,删除这些临时文件(无论如何,对我而言)是可以的。 您可以在OnLaunched方法内的App.xaml.cs文件中执行此操作:
if (args.PreviousExecutionState == ApplicationExecutionState.ClosedByUser)
{
IReadOnlyList<StorageFile> thefiles;
var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longtime");
thefiles = await localFolder.GetFilesAsync();
foreach (var f in thefiles)
{
await f.DeleteAsync(StorageDeleteOption.Default);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.