Files
CreamInstaller/CreamInstaller/Utility/Diagnostics.cs
T
Frog ee19990b5b Logging Infrastructure / Normalize Paths
- Added base logging infrastructure
- Create scan log during library/game scan in %ProgramData%\CreamInstaller\scan.log
- Normalize Steam library paths (libraryfolders.vdf) using Path.GetFullPath + ResolvePath to handle slashes, casing, and drive changes
- Diagnostics.ResolvePath: wrap GetFileSystemInfos in try/catch and guard against empty results to prevent IndexOutOfRangeException (May assist with issues on slow or intermittently accessible external drives)
2026-03-24 00:23:08 -07:00

69 lines
2.4 KiB
C#

using System;
using System.Diagnostics;
using System.IO;
using Microsoft.Win32;
namespace CreamInstaller.Utility;
internal static class Diagnostics
{
private static string nppPath;
private static string NppPath
{
get
{
nppPath ??= Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Notepad++", "", null) as string;
nppPath ??= Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432NODE\Notepad++", "", null) as string;
return nppPath;
}
}
internal static string GetNotepadPath()
{
string npp = NppPath + @"\notepad++.exe";
return npp.FileExists() ? npp : Environment.GetFolderPath(Environment.SpecialFolder.Windows) + @"\notepad.exe";
}
internal static void OpenFileInNotepad(string path)
{
string npp = NppPath + @"\notepad++.exe";
if (npp.FileExists())
OpenFileInNotepadPlusPlus(npp, path);
else
OpenFileInWindowsNotepad(path);
}
private static void OpenFileInNotepadPlusPlus(string npp, string path) =>
Process.Start(new ProcessStartInfo { FileName = npp, Arguments = path });
private static void OpenFileInWindowsNotepad(string path) => Process.Start(new ProcessStartInfo
{ FileName = "notepad.exe", Arguments = path });
internal static void OpenDirectoryInFileExplorer(string path) => Process.Start(new ProcessStartInfo
{ FileName = "explorer.exe", Arguments = path });
internal static void OpenUrlInInternetBrowser(string url) =>
Process.Start(new ProcessStartInfo { FileName = url, UseShellExecute = true });
internal static string ResolvePath(this string path)
{
if (path is null || !path.FileExists() && !path.DirectoryExists())
return null;
DirectoryInfo info = new(path);
if (info.Parent is null)
return info.Name.ToUpperInvariant();
string parent = ResolvePath(info.Parent.FullName);
try
{
FileSystemInfo[] infos = info.Parent.GetFileSystemInfos(info.Name);
string name = infos.Length > 0 ? infos[0].Name : info.Name;
return parent is null ? name : Path.Combine(parent, name);
}
catch
{
// Fall back to the raw name if the filesystem call fails (e.g. on a slow external drive)
return parent is null ? info.Name : Path.Combine(parent, info.Name);
}
}
}