Skip to content

Instantly share code, notes, and snippets.

@iguigova
Created June 26, 2012 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iguigova/2996803 to your computer and use it in GitHub Desktop.
Save iguigova/2996803 to your computer and use it in GitHub Desktop.
ContextMenu
public static class CContextMenu
{
public static string Name
{
get { return "Send File Securely"; }
}
public static string Command
{
get { return string.Format("{0}\"{1}\" \"%1\"", (ApplicationDeployment.IsNetworkDeployed) ? "cmd.exe /c " : string.Empty, exePath); }
}
public static string exePath
{
get { return (ApplicationDeployment.IsNetworkDeployed) ? Path.Combine(CDeploymentInfo.AppPath, exeName) : Assembly.GetExecutingAssembly().Location; }
}
public static string exeName
{
get { return (ApplicationDeployment.IsNetworkDeployed) ? CDeploymentInfo.AppName : Assembly.GetExecutingAssembly().GetName().Name; }
}
public static bool IsContextMenuEnabled()
{
return IsContextMenuEnabled(Registry.CurrentUser, Name);
}
public static void EnableContextMenu()
{
if (!IsContextMenuEnabled(Registry.CurrentUser, Name))
ResetContextMenu(Registry.CurrentUser, Name, Command);
}
public static void DisableContextMenu()
{
ResetContextMenu(Registry.CurrentUser, Name, string.Empty);
}
// http://simpcode.blogspot.com/2008/07/c-set-and-unset-auto-start-for-windows.html
// http://stackoverflow.com/questions/4608505/c-sharp-file-assocation-access-to-the-registry-key-hkey-classes-root-is-den
// http://support.microsoft.com/kb/257592
private const string KeyPath = @"Software\Classes\*";
private const string ShellToken = "shell";
private const string CommandToken = "command";
private static string GetContextMenuKey(string name)
{
return string.Format("{0}\\{1}\\{2}", KeyPath, ShellToken, name);
}
private static string GetContextMenuCommandKey(string name)
{
return string.Format("{0}\\{1}", GetContextMenuKey(name), CommandToken);
}
private static bool IsContextMenuEnabled(RegistryKey key, string name)
{
return key.OpenSubKey(GetContextMenuCommandKey(name)) != null;
}
private static void ResetContextMenu(RegistryKey key, string name, string command)
{
if (string.IsNullOrEmpty(command) && IsContextMenuEnabled(key, name))
{
key.DeleteSubKeyTree(GetContextMenuKey(name), false);
}
if (!string.IsNullOrEmpty(command) && !IsContextMenuEnabled(key, name))
{
key.CreateSubKey(GetContextMenuCommandKey(name)).SetValue(string.Empty, command);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment