Files
PowerToys/src/modules/registrypreview/RegistryPreview/MainWindow.Utilities.cs
Stefan Markovic 41a0114efe [EnvVar][Hosts][RegPrev]Decouple and refactor to make it "packable" as nuget package (#32604)
* WIP Hosts - remove deps

* Add consumer app

* Move App and MainWindow to Consumer app. Make Hosts dll

* Try consume it

* Fix errors

* Make it work with custom build targets

* Dependency injection
Refactor
Explicit page creation
Wire missing dependencies

* Fix installer

* Remove unneeded stuff

* Fix build again

* Extract UI and logic from MainWindow to RegistryPreviewMainPage

* Convert to lib
Change namespace to RegistryPreviewUILib
Remove PT deps

* Add exe app and move App.xaml and MainWindow.xaml

* Consume the lib

* Update Hosts package creation

* Fix RegistryPreview package creation

* Rename RegistryPreviewUI back to RegistryPreview

* Back to consuming lib

* Ship icons and assets in nuget packages

* Rename to EnvironmentVariablesUILib and convert to lib

* Add app and consume

* Telemetry

* GPO

* nuget

* Rename HostsPackageConsumer to Hosts and Hosts lib to HostsUILib

* Assets cleanup

* nuget struct

* v0

* assets

* [Hosts] Re-add AppList to Lib Assets, [RegPrev] Copy lib assets to out dir

* Sign UI dlls

* Revert WinUIEx bump

* Cleanup

* Align deps

* version exception dll

* Fix RegistryPreview crashes

* XAML format

* XAML format 2

* Pack .pri files in lib/ dir

---------

Co-authored-by: Darshak Bhatti <dabhatti@microsoft.com>
2024-04-26 18:41:44 +01:00

96 lines
3.0 KiB
C#

// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Windows.Storage;
using WinUIEx;
namespace RegistryPreview
{
public sealed partial class MainWindow : WindowEx
{
private void OpenWindowPlacementFile(string path, string filename)
{
string fileContents = string.Empty;
string storageFile = Path.Combine(path, filename);
if (File.Exists(storageFile))
{
try
{
StreamReader reader = new StreamReader(storageFile);
fileContents = reader.ReadToEnd();
reader.Close();
}
catch
{
// set up default JSON blob
fileContents = "{ }";
}
}
else
{
Task.Run(() => SaveWindowPlacementFile(path, filename)).GetAwaiter().GetResult();
}
try
{
jsonWindowPlacement = Windows.Data.Json.JsonObject.Parse(fileContents);
}
catch
{
// set up default JSON blob
fileContents = "{ }";
jsonWindowPlacement = Windows.Data.Json.JsonObject.Parse(fileContents);
}
}
/// <summary>
/// Save the window placement JSON blob out to a local file
/// </summary>
private async void SaveWindowPlacementFile(string path, string filename)
{
StorageFolder storageFolder = null;
StorageFile storageFile = null;
string fileContents = string.Empty;
try
{
storageFolder = await StorageFolder.GetFolderFromPathAsync(path);
}
catch (FileNotFoundException ex)
{
Debug.WriteLine(ex.Message);
Directory.CreateDirectory(path);
storageFolder = await StorageFolder.GetFolderFromPathAsync(path);
}
try
{
storageFile = await storageFolder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);
}
catch (FileNotFoundException ex)
{
Debug.WriteLine(ex.Message);
storageFile = await storageFolder.CreateFileAsync(filename);
}
try
{
if (jsonWindowPlacement != null)
{
fileContents = jsonWindowPlacement.Stringify();
await Windows.Storage.FileIO.WriteTextAsync(storageFile, fileContents);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
}
}