本文整理汇总了C#中System.Windows.Application.Exit事件的典型用法代码示例。如果您正苦于以下问题:C# Application.Exit事件的具体用法?C# Application.Exit怎么用?C# Application.Exit使用的例子?那么恭喜您, 这里精选的事件代码示例或许可以为您提供帮助。您也可以进一步了解该事件所在类System.Windows.Application
的用法示例。
在下文中一共展示了Application.Exit事件的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: App_Exit
//引入命名空间
using System;
using System.Collections;
using System.Windows;
using System.IO;
using System.IO.IsolatedStorage;
namespace CSharp
{
public enum ApplicationExitCode
{
Success = 0,
Failure = 1,
CantWriteToApplicationLog = 2,
CantPersistApplicationState = 3
}
public partial class App : Application
{
void App_Exit(object sender, ExitEventArgs e)
{
try
{
// Write entry to application log
if (e.ApplicationExitCode == (int)ApplicationExitCode.Success)
{
WriteApplicationLogEntry("Failure", e.ApplicationExitCode);
}
else
{
WriteApplicationLogEntry("Success", e.ApplicationExitCode);
}
}
catch
{
// Update exit code to reflect failure to write to application log
e.ApplicationExitCode = (int)ApplicationExitCode.CantWriteToApplicationLog;
}
// Persist application state
try
{
PersistApplicationState();
}
catch
{
// Update exit code to reflect failure to persist application state
e.ApplicationExitCode = (int)ApplicationExitCode.CantPersistApplicationState;
}
}
void WriteApplicationLogEntry(string message, int exitCode)
{
// Write log entry to file in isolated storage for the user
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForAssembly();
using (Stream stream = new IsolatedStorageFileStream("log.txt", FileMode.Append, FileAccess.Write, store))
using (StreamWriter writer = new StreamWriter(stream))
{
string entry = string.Format("{0}: {1} - {2}", message, exitCode, DateTime.Now);
writer.WriteLine(entry);
}
}
void PersistApplicationState()
{
// Persist application state to file in isolated storage for the user
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForAssembly();
using (Stream stream = new IsolatedStorageFileStream("state.txt", FileMode.Create, store))
using (StreamWriter writer = new StreamWriter(stream))
{
foreach (DictionaryEntry entry in this.Properties)
{
writer.WriteLine(entry.Value);
}
}
}
}
}