Skip to content

Commit

Permalink
✨ Registry2
Browse files Browse the repository at this point in the history
  • Loading branch information
AigioL committed Jan 21, 2024
1 parent 008c2ed commit ad61bfe
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/BD.Common8.Bcl/Microsoft.Win32/Registry2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,43 @@ public static bool DeleteRegistryKey(string encodedPath, RegistryView view = Def

#endif
}

partial class Registry2
{
/// <summary>
/// %windir%
/// </summary>
static readonly Lazy<string> _windir = new(() =>
{
var windir = Environment.GetFolderPath(Environment.SpecialFolder.Windows);
return windir.ThrowIsNull();
});

static readonly Lazy<string> _regedit_exe = new(() =>
{
var regedit_exe = Path.Combine(_windir!.Value, "regedit.exe");
return regedit_exe;
});

/// <summary>
/// %windir%\regedit.exe
/// </summary>
public static string Regedit => _regedit_exe.Value;

/// <summary>
/// 带参数(可选/null)启动 %windir%\regedit.exe 并等待退出后删除文件
/// </summary>
public static async Task StartProcessRegeditAsync(
string path,
string contents,
int millisecondsDelay = 3700,
string? markKey = null,
string? markValue = null)
{
File.WriteAllText(path, contents, Encoding.UTF8);
var args = $"/s \"{path}\"";
var p = Process2.Start(Regedit, args, workingDirectory: _windir.Value);
await IOPath.TryDeleteInDelayAsync(p, path, millisecondsDelay, millisecondsDelay);
}
}
#endif

0 comments on commit ad61bfe

Please sign in to comment.