window store app 附件读取

时间:2022-04-17 17:12:23
   public static async Task<bool> DisplayApplicationPicker(string folderName, string fileName)
{
// Path to the file in the app package to launch MessageDialog message = null;
StorageFolder storageFolder = null;
StorageFile GetStorageFile = null; IReadOnlyList<StorageFolder> storageFolders; bool isExist = false;
try
{ try
{ //在指定的应用程序文件夹下查找指定的文件
storageFolder = ApplicationData.Current.LocalFolder;
storageFolders = await storageFolder.GetFoldersAsync();
isExist = storageFolders.Any(folder => folder.Name == folderName);
if (isExist)
{
storageFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync(folderName); }
else
{
storageFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(folderName);
} }
catch (System.IO.FileNotFoundException ex)
{
message = new MessageDialog(ex.Message);
message.ShowAsync();
}
//判断是否有该文件
try
{
//在指定的应用程序文件夹下查找指定的文件
if (isExist)
{
IReadOnlyList<StorageFile> files = await storageFolder.GetFilesAsync();
bool isExistfile = files.Any(file => file.Name == fileName);
if (!isExistfile)
{
message = new MessageDialog("Didn't find the specified file");
message.ShowAsync();
//todo: Written to the file Method
}
else
{
GetStorageFile = await storageFolder.GetFileAsync(fileName);
} } }
catch (System.IO.FileNotFoundException ex)
{
message = new MessageDialog(ex.Message);
message.ShowAsync();
} if (GetStorageFile != null)
{
// Set the option to show the picker
var options = new Windows.System.LauncherOptions();
options.DisplayApplicationPicker = true; // Launch the retrieved file
bool success = await Windows.System.Launcher.LaunchFileAsync(GetStorageFile, options); if (success)
{
// File launched
return true; }
else
{
// File launch failed
message = new MessageDialog("Please choose other open way");
message.ShowAsync();
return false; }
}
else
{
message = new MessageDialog( "Didn't find the specified file");
message.ShowAsync();
return false;
} }
catch (Exception ex)
{
message = new MessageDialog(ex.Message);
message.ShowAsync();
return false;
} }