public async override void GetSpecialProperties() { ViewModel.IsReadOnly = NativeFileOperationsHelper.HasFileAttribute(Library.ItemPath, System.IO.FileAttributes.ReadOnly); ViewModel.IsHidden = NativeFileOperationsHelper.HasFileAttribute(Library.ItemPath, System.IO.FileAttributes.Hidden); var fileIconData = await FileThumbnailHelper.LoadIconWithoutOverlayAsync(Library.ItemPath, 80); if (fileIconData != null) { ViewModel.IconData = fileIconData; ViewModel.LoadCustomIcon = false; ViewModel.LoadFileIcon = true; } BaseStorageFile libraryFile = await AppInstance.FilesystemViewModel.GetFileFromPathAsync(Library.ItemPath); if (libraryFile != null) { ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings; string returnformat = Enum.Parse <TimeStyle>(localSettings.Values[Constants.LocalSettings.DateTimeFormat].ToString()) == TimeStyle.Application ? "D" : "g"; ViewModel.ItemCreatedTimestamp = libraryFile.DateCreated.GetFriendlyDateFromFormat(returnformat); if (libraryFile.Properties != null) { GetOtherProperties(libraryFile.Properties); } } var storageFolders = new List <BaseStorageFolder>(); if (Library.Folders != null) { try { foreach (var path in Library.Folders) { BaseStorageFolder folder = await AppInstance.FilesystemViewModel.GetFolderFromPathAsync(path); if (!string.IsNullOrEmpty(folder.Path)) { storageFolders.Add(folder); } } } catch (Exception ex) { App.Logger.Warn(ex, ex.Message); } } if (storageFolders.Count > 0) { ViewModel.ContainsFilesOrFolders = true; ViewModel.LocationsCount = storageFolders.Count; GetLibrarySize(storageFolders, TokenSource.Token); } else { ViewModel.FilesAndFoldersCountString = "LibraryNoLocations/Text".GetLocalized(); } }
public override IAsyncOperation <IInputStream> OpenSequentialReadAsync() { return(AsyncInfo.Run <IInputStream>(async(cancellationToken) => { var hFile = NativeFileOperationsHelper.OpenFileForRead(ContainerPath); if (hFile.IsInvalid) { return null; } if (Path == ContainerPath) { return new FileStream(hFile, FileAccess.Read).AsInputStream(); } ZipFile zipFile = new ZipFile(new FileStream(hFile, FileAccess.Read)); zipFile.IsStreamOwner = true; var znt = new ZipNameTransform(ContainerPath); var entry = zipFile.GetEntry(znt.TransformFile(Path)); if (entry != null) { return new InputStreamWithDisposeCallback(zipFile.GetInputStream(entry)) { DisposeCallback = () => zipFile.Close() }; } return null; })); }
public override IAsyncOperation <IStorageItem> GetItemAsync(string name) { return(AsyncInfo.Run <IStorageItem>(async(cancellationToken) => { var hFile = NativeFileOperationsHelper.OpenFileForRead(ContainerPath); if (hFile.IsInvalid) { return null; } using (ZipFile zipFile = new ZipFile(new FileStream(hFile, FileAccess.Read))) { zipFile.IsStreamOwner = true; ZipEncoding ??= DetectFileEncoding(zipFile); var entry = zipFile.GetEntry(name); if (entry != null) { var wnt = new WindowsNameTransform(ContainerPath); if (entry.IsDirectory) { return new ZipStorageFolder(wnt.TransformDirectory(DecodeEntryName(entry, ZipEncoding)), ContainerPath, entry) { ZipEncoding = ZipEncoding }; } else { return new ZipStorageFile(wnt.TransformFile(DecodeEntryName(entry, ZipEncoding)), ContainerPath, entry); } } return null; } })); }
public override async void GetSpecialProperties() { ViewModel.IsReadOnly = NativeFileOperationsHelper.HasFileAttribute( Item.ItemPath, System.IO.FileAttributes.ReadOnly); ViewModel.IsHidden = NativeFileOperationsHelper.HasFileAttribute( Item.ItemPath, System.IO.FileAttributes.Hidden); ViewModel.ItemSizeVisibility = Visibility.Visible; ViewModel.ItemSize = $"{ByteSize.FromBytes(Item.FileSizeBytes).ToBinaryString().ConvertSizeAbbreviation()} ({ByteSize.FromBytes(Item.FileSizeBytes).Bytes:#,##0} {"ItemSizeBytes".GetLocalized()})"; var fileIconInfo = await AppInstance.FilesystemViewModel.LoadIconOverlayAsync(Item.ItemPath, 80); if (fileIconInfo.IconData != null) { ViewModel.FileIconSource = await fileIconInfo.IconData.ToBitmapAsync(); } if (Item.IsShortcutItem) { ViewModel.ItemCreatedTimestamp = Item.ItemDateCreated; ViewModel.ItemAccessedTimestamp = Item.ItemDateAccessed; ViewModel.LoadLinkIcon = Item.LoadWebShortcutGlyph; if (Item.IsLinkItem || string.IsNullOrWhiteSpace(((ShortcutItem)Item).TargetPath)) { // Can't show any other property return; } } StorageFile file = await AppInstance.FilesystemViewModel.GetFileFromPathAsync((Item as ShortcutItem)?.TargetPath ?? Item.ItemPath); if (file == null) { // Could not access file, can't show any other property return; } if (Item.IsShortcutItem) { // Can't show any other property return; } GetOtherProperties(file.Properties); // Get file MD5 hash var hashAlgTypeName = HashAlgorithmNames.Md5; ViewModel.ItemMD5HashProgressVisibility = Visibility.Visible; ViewModel.ItemMD5HashVisibility = Visibility.Visible; try { ViewModel.ItemMD5Hash = await GetHashForFileAsync(Item, hashAlgTypeName, TokenSource.Token, ProgressBar, AppInstance); } catch (Exception ex) { NLog.LogManager.GetCurrentClassLogger().Warn(ex, ex.Message); ViewModel.ItemMD5HashCalcError = true; } }
private void ViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { switch (e.PropertyName) { case "IsReadOnly": if (ViewModel.IsReadOnly) { List.ForEach(x => NativeFileOperationsHelper.SetFileAttribute( x.ItemPath, System.IO.FileAttributes.ReadOnly)); } else { List.ForEach(x => NativeFileOperationsHelper.UnsetFileAttribute( x.ItemPath, System.IO.FileAttributes.ReadOnly)); } break; case "IsHidden": if (ViewModel.IsHidden) { List.ForEach(x => NativeFileOperationsHelper.SetFileAttribute( x.ItemPath, System.IO.FileAttributes.Hidden)); } else { List.ForEach(x => NativeFileOperationsHelper.UnsetFileAttribute( x.ItemPath, System.IO.FileAttributes.Hidden)); } break; } }
public override IAsyncOperation <IRandomAccessStreamWithContentType> OpenReadAsync() { return(AsyncInfo.Run <IRandomAccessStreamWithContentType>(async(cancellationToken) => { var hFile = NativeFileOperationsHelper.OpenFileForRead(ContainerPath); if (hFile.IsInvalid) { return null; } if (Path == ContainerPath) { return new StreamWithContentType(new FileStream(hFile, FileAccess.Read).AsRandomAccessStream()); } ZipFile zipFile = new ZipFile(new FileStream(hFile, FileAccess.Read)); zipFile.IsStreamOwner = true; var znt = new ZipNameTransform(ContainerPath); var entry = zipFile.GetEntry(znt.TransformFile(Path)); if (entry != null) { var nsStream = new NonSeekableRandomAccessStream(zipFile.GetInputStream(entry), (ulong)entry.Size) { DisposeCallback = () => zipFile.Close() }; return new StreamWithContentType(nsStream); } return null; })); }
public override IAsyncOperation <BaseStorageFile> CopyAsync(IStorageFolder destinationFolder, string desiredNewName, NameCollisionOption option) { return(AsyncInfo.Run <BaseStorageFile>(async(cancellationToken) => { var hFile = NativeFileOperationsHelper.OpenFileForRead(ContainerPath); if (hFile.IsInvalid) { return null; } using (ZipFile zipFile = new ZipFile(new FileStream(hFile, FileAccess.Read))) { zipFile.IsStreamOwner = true; var znt = new ZipNameTransform(ContainerPath); var entry = zipFile.GetEntry(znt.TransformFile(Path)); if (entry != null) { var destFolder = destinationFolder.AsBaseStorageFolder(); var destFile = await destFolder.CreateFileAsync(desiredNewName, option.Convert()); using (var inStream = zipFile.GetInputStream(entry)) using (var outStream = await destFile.OpenStreamForWriteAsync()) { await inStream.CopyToAsync(outStream); await outStream.FlushAsync(); } return destFile; } return null; } })); }
public override IAsyncAction CopyAndReplaceAsync(IStorageFile fileToReplace) { return(AsyncInfo.Run(async(cancellationToken) => { var hFile = NativeFileOperationsHelper.OpenFileForRead(ContainerPath); if (hFile.IsInvalid) { return; } using (ZipFile zipFile = new ZipFile(new FileStream(hFile, FileAccess.Read))) { zipFile.IsStreamOwner = true; var znt = new ZipNameTransform(ContainerPath); var entry = zipFile.GetEntry(znt.TransformFile(Path)); if (entry != null) { using var hDestFile = fileToReplace.CreateSafeFileHandle(FileAccess.ReadWrite); using (var inStream = zipFile.GetInputStream(entry)) using (var outStream = new FileStream(hDestFile, FileAccess.Write)) { await inStream.CopyToAsync(outStream); await outStream.FlushAsync(); } } } })); }
public override IAsyncOperation <BaseStorageFile> CopyAsync(IStorageFolder destinationFolder, string desiredNewName, NameCollisionOption option) { return(AsyncInfo.Run <BaseStorageFile>(async(cancellationToken) => { if (string.IsNullOrEmpty(destinationFolder.Path)) { throw new NotSupportedException(); } var destination = IO.Path.Combine(destinationFolder.Path, desiredNewName); var destFile = new NativeStorageFile(destination, desiredNewName, DateTime.Now); if (!IsAlternateStream) { if (!await Task.Run(() => NativeFileOperationsHelper.CopyFileFromApp(Path, destination, option != NameCollisionOption.ReplaceExisting))) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } else { destFile.CreateFile(); using (var inStream = await this.OpenStreamForReadAsync()) using (var outStream = await destFile.OpenStreamForWriteAsync()) { await inStream.CopyToAsync(outStream); await outStream.FlushAsync(); } } return destFile; })); }
private static IEnumerable <ListedItem> EnumAdsForPath(string itemPath, ListedItem main) { foreach (var ads in NativeFileOperationsHelper.GetAlternateStreams(itemPath)) { yield return(GetAlternateStream(ads, main)); } }
public override IAsyncAction RenameAsync(string desiredName, NameCollisionOption option) { return(AsyncInfo.Run(async(cancellationToken) => { string destination = IO.Path.Combine(IO.Path.GetDirectoryName(Path), desiredName); var destFile = new NativeStorageFile(destination, desiredName, DateTime.Now); if (!IsAlternateStream) { if (!await Task.Run(() => NativeFileOperationsHelper.MoveFileFromApp(Path, destination))) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } else { destFile.CreateFile(); using (var inStream = await this.OpenStreamForReadAsync()) using (var outStream = await destFile.OpenStreamForWriteAsync()) { await inStream.CopyToAsync(outStream); await outStream.FlushAsync(); } await DeleteAsync(); } })); }
private Func <IRandomAccessStream, IAsyncOperation <bool> > WriteZipEntry(ZipFile zipFile) { return((stream) => AsyncInfo.Run((cancellationToken) => Task.Run(() => { var hFile = NativeFileOperationsHelper.OpenFileForRead(ContainerPath, true); if (hFile.IsInvalid) { return true; } try { var znt = new ZipNameTransform(ContainerPath); var zipDesiredName = znt.TransformFile(Path); var entry = zipFile.GetEntry(zipDesiredName); zipFile.BeginUpdate(new MemoryArchiveStorage(FileUpdateMode.Direct)); if (entry != null) { zipFile.Delete(entry); } zipFile.Add(new StreamDataSource(stream), zipDesiredName); zipFile.CommitUpdate(); } catch (Exception ex) { App.Logger.Warn(ex, "Error writing zip file"); } return true; }))); }
public override IAsyncOperation <IInputStream> OpenSequentialReadAsync() { return(AsyncInfo.Run(async(cancellationToken) => { var hFile = NativeFileOperationsHelper.OpenFileForRead(Path); return new FileStream(hFile, FileAccess.Read).AsInputStream(); })); }
public override IAsyncOperation <IRandomAccessStream> OpenAsync(FileAccessMode accessMode) { return(AsyncInfo.Run <IRandomAccessStream>(async(cancellationToken) => { var hFile = NativeFileOperationsHelper.OpenFileForRead(Path, accessMode == FileAccessMode.ReadWrite); return new FileStream(hFile, accessMode == FileAccessMode.ReadWrite ? FileAccess.ReadWrite : FileAccess.Read).AsRandomAccessStream(); })); }
private void CreateFile() { using var hFile = NativeFileOperationsHelper.CreateFileForWrite(Path, false); if (hFile.IsInvalid) { throw new Win32Exception(Marshal.GetLastWin32Error()); } }
private async void ViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { switch (e.PropertyName) { case "IsReadOnly": if (ViewModel.IsReadOnly) { NativeFileOperationsHelper.SetFileAttribute( Item.ItemPath, System.IO.FileAttributes.ReadOnly); } else { NativeFileOperationsHelper.UnsetFileAttribute( Item.ItemPath, System.IO.FileAttributes.ReadOnly); } break; case "IsHidden": if (ViewModel.IsHidden) { NativeFileOperationsHelper.SetFileAttribute( Item.ItemPath, System.IO.FileAttributes.Hidden); } else { NativeFileOperationsHelper.UnsetFileAttribute( Item.ItemPath, System.IO.FileAttributes.Hidden); } break; case "ShortcutItemPath": case "ShortcutItemWorkingDir": case "ShortcutItemArguments": var tmpItem = (ShortcutItem)Item; if (string.IsNullOrWhiteSpace(ViewModel.ShortcutItemPath)) { return; } var connection = await AppServiceConnectionHelper.Instance; if (connection != null) { var value = new ValueSet() { { "Arguments", "FileOperation" }, { "fileop", "UpdateLink" }, { "filepath", Item.ItemPath }, { "targetpath", ViewModel.ShortcutItemPath }, { "arguments", ViewModel.ShortcutItemArguments }, { "workingdir", ViewModel.ShortcutItemWorkingDir }, { "runasadmin", tmpItem.RunAsAdmin }, }; await connection.SendMessageAsync(value); } break; } }
public override IAsyncAction DeleteAsync() { return(AsyncInfo.Run(async(cancellationToken) => { if (!NativeFileOperationsHelper.DeleteFileFromApp(Path)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } })); }
public static void WriteFileTag(string filePath, string tag) { if (tag == null) { NativeFileOperationsHelper.DeleteFileFromApp($"{filePath}:files"); } else if (ReadFileTag(filePath) != tag) { NativeFileOperationsHelper.WriteStringToFile($"{filePath}:files", tag); } }
private static bool CheckAccess(string path) { return(SafetyExtensions.IgnoreExceptions(() => { var hFile = NativeFileOperationsHelper.OpenFileForRead(path); if (hFile.IsInvalid) { return false; } using var stream = new FileStream(hFile, FileAccess.Read); return CheckAccess(stream); })); }
public override IAsyncOperation <IReadOnlyList <IStorageItem> > GetItemsAsync() { return(AsyncInfo.Run <IReadOnlyList <IStorageItem> >(async(cancellationToken) => { var hFile = NativeFileOperationsHelper.OpenFileForRead(ContainerPath); if (hFile.IsInvalid) { return null; } using (ZipFile zipFile = new ZipFile(new FileStream(hFile, FileAccess.Read))) { zipFile.IsStreamOwner = true; ZipEncoding ??= DetectFileEncoding(zipFile); var wnt = new WindowsNameTransform(ContainerPath, true); // Check with Path.GetFullPath var items = new List <IStorageItem>(); foreach (var entry in zipFile.OfType <ZipEntry>()) // Returns all items recursively { string winPath = System.IO.Path.GetFullPath(entry.IsDirectory ? wnt.TransformDirectory(DecodeEntryName(entry, ZipEncoding)) : wnt.TransformFile(DecodeEntryName(entry, ZipEncoding))); if (winPath.StartsWith(Path.WithEnding("\\"))) // Child of self { var split = winPath.Substring(Path.Length).Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries); if (split.Length > 0) { if (entry.IsDirectory || split.Length > 1) // Not all folders have a ZipEntry { var itemPath = System.IO.Path.Combine(Path, split[0]); if (!items.Any(x => x.Path == itemPath)) { items.Add(new ZipStorageFolder(itemPath, ContainerPath, entry) { ZipEncoding = ZipEncoding }); } } else { items.Add(new ZipStorageFile(winPath, ContainerPath, entry)); } } } } return items; } })); }
public override async void GetSpecialProperties() { if (List.All(x => x.PrimaryItemAttribute == StorageItemTypes.File)) { ViewModel.IsReadOnly = List.All(x => NativeFileOperationsHelper.HasFileAttribute(x.ItemPath, System.IO.FileAttributes.ReadOnly)); } ViewModel.IsHidden = List.All(x => NativeFileOperationsHelper.HasFileAttribute(x.ItemPath, System.IO.FileAttributes.Hidden)); ViewModel.LastSeparatorVisibility = Visibility.Collapsed; ViewModel.ItemSizeVisibility = Visibility.Visible; ViewModel.FilesCount += List.Where(x => x.PrimaryItemAttribute == StorageItemTypes.File).ToList().Count; ViewModel.FoldersCount += List.Where(x => x.PrimaryItemAttribute == StorageItemTypes.Folder).ToList().Count; long totalSize = 0; long filesSize = List.Where(x => x.PrimaryItemAttribute == StorageItemTypes.File).Sum(x => x.FileSizeBytes); long foldersSize = 0; ViewModel.ItemSizeProgressVisibility = Visibility.Visible; foreach (var item in List) { if (item.PrimaryItemAttribute == StorageItemTypes.Folder) { var fileSizeTask = Task.Run(async() => { var size = await CalculateFolderSizeAsync(item.ItemPath, TokenSource.Token); return(size); }); try { foldersSize += await fileSizeTask; } catch (Exception ex) { NLog.LogManager.GetCurrentClassLogger().Error(ex, ex.Message); } } } ViewModel.ItemSizeProgressVisibility = Visibility.Collapsed; totalSize = filesSize + foldersSize; ViewModel.ItemSize = ByteSize.FromBytes(totalSize).ToBinaryString().ConvertSizeAbbreviation() + " (" + ByteSize.FromBytes(totalSize).Bytes.ToString("#,##0") + " " + "ItemSizeBytes".GetLocalized() + ")"; SetItemsCountString(); }
public override async void GetSpecialProperties() { if (List.All(x => x.PrimaryItemAttribute == StorageItemTypes.File)) { ViewModel.IsReadOnly = List.All(x => NativeFileOperationsHelper.HasFileAttribute(x.ItemPath, System.IO.FileAttributes.ReadOnly)); } ViewModel.IsHidden = List.All(x => NativeFileOperationsHelper.HasFileAttribute(x.ItemPath, System.IO.FileAttributes.Hidden)); ViewModel.LastSeparatorVisibility = false; ViewModel.ItemSizeVisibility = true; ViewModel.FilesCount += List.Where(x => x.PrimaryItemAttribute == StorageItemTypes.File).ToList().Count; ViewModel.FoldersCount += List.Where(x => x.PrimaryItemAttribute == StorageItemTypes.Folder).ToList().Count; long totalSize = 0; long filesSize = List.Where(x => x.PrimaryItemAttribute == StorageItemTypes.File).Sum(x => x.FileSizeBytes); long foldersSize = 0; ViewModel.ItemSizeProgressVisibility = true; foreach (var item in List) { if (item.PrimaryItemAttribute == StorageItemTypes.Folder) { var fileSizeTask = Task.Run(async() => { var size = await CalculateFolderSizeAsync(item.ItemPath, TokenSource.Token); return(size); }); try { foldersSize += await fileSizeTask; } catch (Exception ex) { App.Logger.Warn(ex, ex.Message); } } } ViewModel.ItemSizeProgressVisibility = false; totalSize = filesSize + foldersSize; ViewModel.ItemSize = totalSize.ToLongSizeString(); SetItemsCountString(); }
private StreamedFileDataRequestedHandler ZipDataStreamingHandler(string name) { return(async request => { try { // If called from here it fails with Access Denied?! //var hFile = NativeFileOperationsHelper.OpenFileForRead(ContainerPath); var hFile = await NativeFileOperationsHelper.OpenProtectedFileForRead(ContainerPath); if (hFile.IsInvalid) { request.FailAndClose(StreamedFileFailureMode.CurrentlyUnavailable); return; } using (ZipFile zipFile = new ZipFile(new FileStream(hFile, FileAccess.Read))) { zipFile.IsStreamOwner = true; var znt = new ZipNameTransform(ContainerPath); var entry = zipFile.GetEntry(znt.TransformFile(name)); if (entry != null) { using (var inStream = zipFile.GetInputStream(entry)) using (var outStream = request.AsStreamForWrite()) { await inStream.CopyToAsync(outStream); await outStream.FlushAsync(); } request.Dispose(); } else { request.FailAndClose(StreamedFileFailureMode.CurrentlyUnavailable); } } } catch { request.FailAndClose(StreamedFileFailureMode.Failed); } }); }
public virtual void ImportSettings(object import) { try { // Try convert settingsCache = (Dictionary <string, object>)import; // Serialize string serialized = JsonConvert.SerializeObject(settingsCache, Formatting.Indented); // Write to file NativeFileOperationsHelper.WriteStringToFile(settingsPath, serialized); } catch (Exception ex) { Debug.WriteLine(ex); Debugger.Break(); } }
private static bool CheckAccess(string path) { try { var hFile = NativeFileOperationsHelper.OpenFileForRead(path); if (hFile.IsInvalid) { return(false); } using (ZipFile zipFile = new ZipFile(new FileStream(hFile, FileAccess.Read))) { zipFile.IsStreamOwner = true; } return(true); } catch { return(false); } }
private IAsyncOperation <ZipFile> OpenZipFileAsync(FileAccessMode accessMode) { return(AsyncInfo.Run <ZipFile>(async(cancellationToken) => { bool readWrite = accessMode == FileAccessMode.ReadWrite; if (BackingFile != null) { return new ZipFile((await BackingFile.OpenAsync(accessMode)).AsStream()); } else { var hFile = NativeFileOperationsHelper.OpenFileForRead(ContainerPath, readWrite); if (hFile.IsInvalid) { return null; } return new ZipFile((Stream) new FileStream(hFile, readWrite ? FileAccess.ReadWrite : FileAccess.Read)); } })); }
private BaseBasicProperties GetBasicProperties() { var hFile = NativeFileOperationsHelper.OpenFileForRead(ContainerPath); if (hFile.IsInvalid) { return(new BaseBasicProperties()); } using (ZipFile zipFile = new ZipFile(new FileStream(hFile, FileAccess.Read))) { zipFile.IsStreamOwner = true; var znt = new ZipNameTransform(ContainerPath); var entry = zipFile.GetEntry(znt.TransformFile(Path)); if (entry != null) { return(new ZipFileBasicProperties(entry)); } return(new BaseBasicProperties()); } }
public static void WriteFileTag(string filePath, string tag) { var isReadOnly = NativeFileOperationsHelper.HasFileAttribute(filePath, System.IO.FileAttributes.ReadOnly); if (isReadOnly) // Unset read-only attribute (#7534) { NativeFileOperationsHelper.UnsetFileAttribute(filePath, System.IO.FileAttributes.ReadOnly); } if (tag == null) { NativeFileOperationsHelper.DeleteFileFromApp($"{filePath}:files"); } else if (ReadFileTag(filePath) != tag) { NativeFileOperationsHelper.WriteStringToFile($"{filePath}:files", tag); } if (isReadOnly) // Restore read-only attribute (#7534) { NativeFileOperationsHelper.SetFileAttribute(filePath, System.IO.FileAttributes.ReadOnly); } }
protected virtual TValue Get <TValue>(TValue defaultValue, [CallerMemberName] string propertyName = "") { try { string settingsData = NativeFileOperationsHelper.ReadStringFromFile(settingsPath); Dictionary <string, TValue> rawData = JsonConvert.DeserializeObject <Dictionary <string, TValue> >(settingsData); Dictionary <string, object> convertedData = new Dictionary <string, object>(); if (rawData != null) { foreach (var item in rawData) { convertedData.Add(item.Key, (TValue)item.Value); } } serializableSettings = convertedData; if (serializableSettings == null) { serializableSettings = new Dictionary <string, object>(); } if (!serializableSettings.ContainsKey(propertyName)) { serializableSettings.Add(propertyName, defaultValue); // Serialize NativeFileOperationsHelper.WriteStringToFile(settingsPath, JsonConvert.SerializeObject(serializableSettings, Formatting.Indented)); } return((TValue)serializableSettings[propertyName]); } catch (Exception e) { Debugger.Break(); return(default(TValue)); } }
public override IAsyncOperation <IRandomAccessStream> OpenAsync(FileAccessMode accessMode) { return(AsyncInfo.Run <IRandomAccessStream>(async(cancellationToken) => { bool rw = accessMode == FileAccessMode.ReadWrite; var hFile = NativeFileOperationsHelper.OpenFileForRead(ContainerPath, rw); if (hFile.IsInvalid) { return null; } if (Path == ContainerPath) { return new FileStream(hFile, FileAccess.Read).AsRandomAccessStream(); } ZipFile zipFile = new ZipFile(new FileStream(hFile, rw ? FileAccess.ReadWrite : FileAccess.Read)); zipFile.IsStreamOwner = true; var znt = new ZipNameTransform(ContainerPath); var entry = zipFile.GetEntry(znt.TransformFile(Path)); if (!rw) { if (entry != null) { return new NonSeekableRandomAccessStream(zipFile.GetInputStream(entry), (ulong)entry.Size) { DisposeCallback = () => zipFile.Close() }; } } else { return new RandomAccessStreamWithFlushCallback() { DisposeCallback = () => zipFile.Close(), FlushCallback = WriteZipEntry(zipFile) }; } return null; })); }