Skip to content

Commit

Permalink
Make sure config and backups are read/written next to the executable
Browse files Browse the repository at this point in the history
Those files would have been written to the current working dir when the tool was launched from a shell
  • Loading branch information
Fs00 authored Aug 17, 2023
1 parent e9e83d1 commit f5e5187
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
9 changes: 4 additions & 5 deletions src/AppConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ namespace Win10BloatRemover;

public class AppConfiguration
{
private const string CONFIGURATION_FILE_NAME = "config.json";

public required string[] ServicesToRemove { get; init; }
public required UwpAppGroup[] UWPAppsToRemove { get; init; }
public required UwpAppRemovalMode UWPAppsRemovalMode { get; init; }
public required string[] ScheduledTasksToDisable { get; init; }
public required string[] WindowsFeaturesToRemove { get; init; }

private static readonly string configurationFilePath = Path.Join(AppContext.BaseDirectory, "config.json");
private static readonly JsonSerializerOptions serializerOptions = new() {
AllowTrailingCommas = true,
Converters = { new JsonStringEnumConverter() },
Expand All @@ -25,7 +24,7 @@ public class AppConfiguration

public static AppConfiguration LoadOrCreateFile()
{
if (File.Exists(CONFIGURATION_FILE_NAME))
if (File.Exists(configurationFilePath))
{
var loadedConfiguration = LoadFromFile();
return loadedConfiguration;
Expand All @@ -39,7 +38,7 @@ private static AppConfiguration LoadFromFile()
{
try
{
string settingsFileContent = File.ReadAllText(CONFIGURATION_FILE_NAME, Encoding.UTF8);
string settingsFileContent = File.ReadAllText(configurationFilePath, Encoding.UTF8);
var parsedConfiguration = JsonSerializer.Deserialize<AppConfiguration>(settingsFileContent, serializerOptions);
if (parsedConfiguration == null)
throw new Exception("The file does not contain a valid configuration.");
Expand All @@ -56,7 +55,7 @@ private void WriteToFile()
try
{
byte[] settingsFileContent = JsonSerializer.SerializeToUtf8Bytes(this, serializerOptions);
File.WriteAllBytes(CONFIGURATION_FILE_NAME, settingsFileContent);
File.WriteAllBytes(configurationFilePath, settingsFileContent);
}
catch (Exception exc)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Operations/ServiceRemover.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public ServiceRemover(IUserInterface ui) : this(ui, DateTime.Now) {}
public ServiceRemover(IUserInterface ui, DateTime now)
{
this.ui = ui;
backupDirectory = new DirectoryInfo($"servicesBackup_{now:yyyy-MM-dd_HH-mm-ss}");
string backupDirectoryPath = Path.Join(AppContext.BaseDirectory, $"servicesBackup_{now:yyyy-MM-dd_HH-mm-ss}");
backupDirectory = new DirectoryInfo(backupDirectoryPath);
}

public void BackupAndRemove(params string[] servicesToRemove)
Expand Down

0 comments on commit f5e5187

Please # to comment.