csharp-右键菜单

添加到目录的右键菜单

// 添加到目录的右键菜单
RegistryKey shell = Registry.ClassesRoot.OpenSubKey(@"directory\shell", true);
var keys = shell.GetSubKeyNames();
bool isPresent = false;
foreach (string key in keys)
{
if (key == "Cleaner")
{
// already added
isPresent = true;
break;
}
}
if (!isPresent)
{
RegistryKey appName = shell.CreateSubKey("Cleaner");
RegistryKey cmd = appName.CreateSubKey("command");
cmd.SetValue("", System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName + " %1"); // %1 是传入的路径参数
cmd.Close();
appName.Close();
}
shell.Close();

添加到文件的右键菜单

// 添加到目录的右键菜单
RegistryKey shell = Registry.ClassesRoot.OpenSubKey(@"*\shell", true);
var keys = shell.GetSubKeyNames();
bool isPresent = false;
foreach (string key in keys)
{
if (key == "Cleaner")
{
// already added
isPresent = true;
break;
}
}
if (!isPresent)
{
RegistryKey appName = shell.CreateSubKey("Cleaner");
RegistryKey cmd = appName.CreateSubKey("command");
cmd.SetValue("", System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName + " %1"); // %1 是传入的路径参数
cmd.Close();
appName.Close();
}
shell.Close();