判断当前程序的类型(UWP/Win32)及路径
开发的程序可能又多个版本和类型,根据程序的不同类型来处理对应的数据和配置档。
如判断程序为UWP/Win32类型,来操作对应的 settings 数据。
string curAppDataPath = string.Empty;
string curAppInstallPath = string.Empty;
try
{
// 当前UWP程序数据的 LocalState 目录路径
curAppDataPath = Windows.Storage.ApplicationData.Current.LocalFolder.Path + "\\";
// 当前UWP程序的安装路径
curAppInstallPath = Windows.ApplicationModel.Package.Current.InstalledLocation.Path + "\\";
Console.WriteLine("Current App Type = UWP");
}
catch
{
// 此时指的是 C:\ProgramData\
curAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\";
// 此处指的是程序安装路径的父路径,即上一层路径
curAppInstallPath = Directory.GetParent(Environment.CurrentDirectory).FullName + "\\";
Console.WriteLine("Current App Type = Win32");
}
其原理为Win32程序如果调用了 UWP的API会抛无效操作的异常,而被 catch住,来到调用Win32对应的API,对应的路径。