Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 0 additions & 144 deletions Network Monitor.Tests/UserConfigImportTests.cs

This file was deleted.

82 changes: 4 additions & 78 deletions Network Monitor/SettingsStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@ public static class SettingsStartup
private const string RepairedArgument = "--repaired";

/// <summary>
/// Brings in another copy's settings if this copy has none, upgrades settings from an older version, and repairs a settings file that can't be read.
/// Upgrades settings from an older version, and repairs a settings file that can't be read.
/// </summary>
/// <returns>False when the app restarted itself to load repaired settings, so this process should exit.</returns>
public static bool Prepare(string[] args)
{
var userConfigPath = TryGetUserConfigPath();

try
{
if (userConfigPath != null)
TryImport(userConfigPath);

if (Settings.Default.MustUpgrade)
{
Settings.Default.Upgrade();
Expand All @@ -36,7 +31,7 @@ public static bool Prepare(string[] args)

return true;
}
catch (ConfigurationErrorsException ex) when (!args.Contains(RepairedArgument) && TryDeleteUnreadable(ex, userConfigPath))
catch (ConfigurationErrorsException ex) when (!args.Contains(RepairedArgument) && TryDeleteUnreadable(ex))
{
// The settings already failed to load in this process and can't be loaded again, so start over in a fresh one.
SingleInstance.Release();
Expand All @@ -46,49 +41,16 @@ public static bool Prepare(string[] args)
}

/// <summary>
/// Returns where this copy keeps its settings for the current version, or null if the existing file can't even be opened, which <see cref="TryDeleteUnreadable" /> deals with.
/// </summary>
private static string TryGetUserConfigPath()
{
try
{
return ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
}
catch (ConfigurationErrorsException)
{
return null;
}
}

private static void TryImport(string userConfigPath)
{
try
{
UserConfigImport.ImportIfNewCopy(userConfigPath, includePlacement: !IsAnotherCopyRunning());
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or System.Xml.XmlException)
{
// Starting from defaults is better than not starting.
}
}

/// <summary>
/// Deletes the settings file that failed to load, as long as it belongs to this copy (the current file or the older one <see cref="Settings.Upgrade" /> reads).
/// Deletes the settings file that failed to load, which is this copy's current one or the older one <see cref="Settings.Upgrade" /> reads.
/// A file cut short by a power loss would otherwise stop the app from starting every time, and reinstalling doesn't remove it.
/// </summary>
private static bool TryDeleteUnreadable(ConfigurationErrorsException exception, string userConfigPath)
private static bool TryDeleteUnreadable(ConfigurationErrorsException exception)
{
var failedFile = (exception.InnerException as ConfigurationErrorsException)?.Filename ?? exception.Filename;

if (string.IsNullOrEmpty(failedFile) || !string.Equals(Path.GetFileName(failedFile), "user.config", StringComparison.OrdinalIgnoreCase))
return false;

// When the current file can't be opened its path isn't known up front, but the failing file names it.
var copyFolder = Path.GetDirectoryName(Path.GetDirectoryName(userConfigPath ?? failedFile));

if (!IsInFolder(failedFile, copyFolder))
return false;

try
{
File.Delete(failedFile);
Expand All @@ -99,40 +61,4 @@ private static bool TryDeleteUnreadable(ConfigurationErrorsException exception,
return false;
}
}

/// <summary>
/// Returns whether <paramref name="path" /> is inside <paramref name="folder" />.
/// </summary>
public static bool IsInFolder(string path, string folder) =>
Path.GetFullPath(path).StartsWith(Path.GetFullPath(folder).TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase);

/// <summary>
/// Returns whether another copy of Network Monitor is running, at any path.
/// </summary>
private static bool IsAnotherCopyRunning()
{
var currentId = Process.GetCurrentProcess().Id;

foreach (var process in Process.GetProcesses())
{
try
{
// Downloads can be renamed (like "Network.Monitor (1)"), so look at what the exe says it is rather than its name.
if (process.Id != currentId &&
process.ProcessName.IndexOf("Network", StringComparison.OrdinalIgnoreCase) >= 0 &&
process.MainModule?.FileVersionInfo.ProductName == "Network Monitor")
return true;
}
catch
{
// Processes we can't inspect, like elevated ones, aren't ours.
}
finally
{
process.Dispose();
}
}

return false;
}
}
122 changes: 0 additions & 122 deletions Network Monitor/UserConfigImport.cs

This file was deleted.

Loading