public static async Task <FileSystemStorageItemBase> OpenAsync(string Path) { if (System.IO.Path.GetPathRoot(Path) != Path && WIN_Native_API.GetStorageItem(Path) is FileSystemStorageItemBase Item) { return(Item); } else { LogTracer.Log($"Native API could not found the path: \"{Path}\", fall back to UWP storage API"); try { string DirectoryPath = System.IO.Path.GetDirectoryName(Path); if (string.IsNullOrEmpty(DirectoryPath)) { StorageFolder Folder = await StorageFolder.GetFolderFromPathAsync(Path); return(await FileSystemStorageFolder.CreateFromExistingStorageItem(Folder)); } else { StorageFolder ParentFolder = await StorageFolder.GetFolderFromPathAsync(DirectoryPath); switch (await ParentFolder.TryGetItemAsync(System.IO.Path.GetFileName(Path))) { case StorageFolder Folder: { return(await FileSystemStorageFolder.CreateFromExistingStorageItem(Folder)); } case StorageFile File: { return(await FileSystemStorageFile.CreateFromExistingStorageItem(File)); } default: { LogTracer.Log($"UWP storage API could not found the path: \"{Path}\""); return(null); } } } } catch (Exception ex) { LogTracer.Log(ex, $"UWP storage API could not found the path: \"{Path}\""); return(null); } } }
public static async Task <FileSystemStorageItemBase> CreateAsync(string Path, StorageItemTypes ItemTypes, CreateOption Option) { switch (ItemTypes) { case StorageItemTypes.File: { if (WIN_Native_API.CreateFileFromPath(Path, Option, out string NewPath)) { return(await OpenAsync(NewPath)); } else { LogTracer.Log($"Native API could not create file: \"{Path}\", fall back to UWP storage API"); try { StorageFolder Folder = await StorageFolder.GetFolderFromPathAsync(System.IO.Path.GetDirectoryName(Path)); switch (Option) { case CreateOption.GenerateUniqueName: { StorageFile NewFile = await Folder.CreateFileAsync(System.IO.Path.GetFileName(Path), CreationCollisionOption.GenerateUniqueName); return(await FileSystemStorageFile.CreateFromExistingStorageItem(NewFile)); } case CreateOption.OpenIfExist: { StorageFile NewFile = await Folder.CreateFileAsync(System.IO.Path.GetFileName(Path), CreationCollisionOption.OpenIfExists); return(await FileSystemStorageFile.CreateFromExistingStorageItem(NewFile)); } case CreateOption.ReplaceExisting: { StorageFile NewFile = await Folder.CreateFileAsync(System.IO.Path.GetFileName(Path), CreationCollisionOption.ReplaceExisting); return(await FileSystemStorageFile.CreateFromExistingStorageItem(NewFile)); } default: { return(null); } } } catch { LogTracer.Log($"UWP storage API could not create file: \"{Path}\""); return(null); } } } case StorageItemTypes.Folder: { if (WIN_Native_API.CreateDirectoryFromPath(Path, Option, out string NewPath)) { return(await OpenAsync(NewPath)); } else { LogTracer.Log($"Native API could not create file: \"{Path}\", fall back to UWP storage API"); try { StorageFolder Folder = await StorageFolder.GetFolderFromPathAsync(System.IO.Path.GetDirectoryName(Path)); switch (Option) { case CreateOption.GenerateUniqueName: { StorageFolder NewFolder = await Folder.CreateFolderAsync(System.IO.Path.GetFileName(Path), CreationCollisionOption.GenerateUniqueName); return(await FileSystemStorageFolder.CreateFromExistingStorageItem(NewFolder)); } case CreateOption.OpenIfExist: { StorageFolder NewFolder = await Folder.CreateFolderAsync(System.IO.Path.GetFileName(Path), CreationCollisionOption.OpenIfExists); return(await FileSystemStorageFolder.CreateFromExistingStorageItem(NewFolder)); } case CreateOption.ReplaceExisting: { StorageFolder NewFolder = await Folder.CreateFolderAsync(System.IO.Path.GetFileName(Path), CreationCollisionOption.ReplaceExisting); return(await FileSystemStorageFolder.CreateFromExistingStorageItem(NewFolder)); } default: { return(null); } } } catch { LogTracer.Log($"UWP storage API could not create folder: \"{Path}\""); return(null); } } } default: { return(null); } } }