LD-Uninstaller/src-csharp/LDUninstaller.Core/Services/ShellIntegrationManager.cs

75 lines
2.3 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.IO;
using Microsoft.Win32;
namespace LDUninstaller.Core.Services
{
public static class ShellIntegrationManager
{
public const string ContextMenuKey = "LDForceUninstall";
public const string MenuText = "💥 LD ile Güçlü Kaldır";
private static readonly string[] Targets = { "Directory", "exefile", "lnkfile" };
public static bool IsRegistered()
{
try
{
using var key = Registry.CurrentUser.OpenSubKey($@"Software\Classes\Directory\shell\{ContextMenuKey}");
return key != null;
}
catch
{
return false;
}
}
public static bool Register(string appExePath)
{
if (string.IsNullOrWhiteSpace(appExePath) || !File.Exists(appExePath))
return false;
try
{
foreach (var target in Targets)
{
string subPath = $@"Software\Classes\{target}\shell\{ContextMenuKey}";
using var menuKey = Registry.CurrentUser.CreateSubKey(subPath);
if (menuKey == null) continue;
menuKey.SetValue("", MenuText, RegistryValueKind.String);
menuKey.SetValue("Icon", $"\"{appExePath}\",0", RegistryValueKind.String);
using var cmdKey = menuKey.CreateSubKey("command");
cmdKey?.SetValue("", $"\"{appExePath}\" --force-path \"%1\"", RegistryValueKind.String);
}
return true;
}
catch
{
return false;
}
}
public static bool Unregister()
{
try
{
foreach (var target in Targets)
{
string subPath = $@"Software\Classes\{target}\shell";
using var shellKey = Registry.CurrentUser.OpenSubKey(subPath, true);
shellKey?.DeleteSubKeyTree(ContextMenuKey, false);
// Eski menü adı varsa onu da temizle
shellKey?.DeleteSubKeyTree("LDUninstaller", false);
}
return true;
}
catch
{
return false;
}
}
}
}