public static void SetAssociation(string Extension, string KeyName, string OpenWith, string FileDescription) { RegistryKey BaseKey; RegistryKey OpenMethod; RegistryKey Shell; RegistryKey CurrentUser; BaseKey = Registry.ClassesRoot.CreateSubKey(Extension); BaseKey.SetValue("", KeyName); OpenMethod = Registry.ClassesRoot.CreateSubKey(KeyName); OpenMethod.SetValue("", FileDescription); OpenMethod.CreateSubKey("DefaultIcon").SetValue("", System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\code.png.ico"); Shell = OpenMethod.CreateSubKey("Shell"); Shell.CreateSubKey("edit").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\""); Shell.CreateSubKey("open").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\""); BaseKey.Close(); OpenMethod.Close(); Shell.Close(); CurrentUser = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + Extension, true); CurrentUser.DeleteSubKey("UserChoice", false); CurrentUser.Close(); SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero); }
// http://stackoverflow.com/questions/2681878/associate-file-extension-with-application //[PrincipalPermission(SecurityAction.Demand, Role = @"BUILTIN\Administrators")] public static void SetAssociation(string Extension, string KeyName, string OpenWith, string FileDescription) { // The stuff that was above here is basically the same RegistryKey BaseKey; RegistryKey OpenMethod; RegistryKey Shell; RegistryKey CurrentUser; BaseKey = Registry.CurrentUser.OpenSubKey("Software\\Classes", true).CreateSubKey(Extension); BaseKey.SetValue("", KeyName); OpenMethod = Registry.CurrentUser.OpenSubKey("Software\\Classes", true).CreateSubKey(KeyName); OpenMethod.SetValue("", FileDescription); OpenMethod.CreateSubKey("DefaultIcon").SetValue("", "\"" + OpenWith + "\",0"); Shell = OpenMethod.CreateSubKey("Shell"); Shell.CreateSubKey("edit").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\""); Shell.CreateSubKey("open").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\""); BaseKey.Close(); OpenMethod.Close(); Shell.Close(); // Delete the key instead of trying to change it CurrentUser = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + Extension, true); CurrentUser.DeleteSubKey("UserChoice", false); CurrentUser.Close(); // Tell explorer the file association has been changed SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero); }
public static void Associate() { /* * RegistryKey FileReg = Registry.ClassesRoot.CreateSubKey(".code"); * RegistryKey AppReg = Registry.CurrentUser.CreateSubKey("Dot DeCode.exe"); * //RegistryKey FileReg = Registry.CurrentUser.CreateSubKey("Software\\Classes\\.code"); * // RegistryKey AppReg = Registry.CurrentUser.CreateSubKey("Software\\Classes\\Applications\\Dot DeCode.exe"); * // RegistryKey AssReg = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\.code"); * * * string configFile = string.Empty; ; * string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); * configFile = Path.Combine(appdata, configFile); * * FileReg.CreateSubKey("DefaultIcon").SetValue("", System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)+ "\\code.png.ico"); * * * AppReg.CreateSubKey("shell\\open\\command").SetValue("", "\"" + System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\Dot DeCode.exe"+"\"%1"); * AppReg.CreateSubKey("DefaultIcon").SetValue("", System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\code.png.ico"); * * //AssReg.CreateSubKey("UserChoice").SetValue("Progid", "Applications\\Dot DeCode.exe"); * * // RegistryKey CurrentUser = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + ".code", true); * // CurrentUser.DeleteSubKey("UserChoice", false); * // CurrentUser.Close(); * * SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero); */ RegistryKey BaseKey; RegistryKey OpenMethod; RegistryKey Shell; RegistryKey CurrentUser; string KeyName = "Dot DeCode"; BaseKey = Registry.ClassesRoot.CreateSubKey(".code"); string configFile = string.Empty;; string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); configFile = Path.Combine(appdata, configFile); BaseKey.SetValue("", KeyName); OpenMethod = Registry.ClassesRoot.CreateSubKey(KeyName); OpenMethod.SetValue("", "CoDe File"); OpenMethod.CreateSubKey("DefaultIcon").SetValue("", System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\code.png.ico"); Shell = OpenMethod.CreateSubKey("Shell"); Shell.CreateSubKey("edit").CreateSubKey("command").SetValue("", "\"" + System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\Dot DeCode.exe" + "\"%1"); Shell.CreateSubKey("open").CreateSubKey("command").SetValue("", "\"" + System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\Dot DeCode.exe" + "\"%1"); BaseKey.Close(); OpenMethod.Close(); Shell.Close(); /* * CurrentUser = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + ".code", true); * CurrentUser.DeleteSubKey("UserChoice", false); * CurrentUser.Close(); */ SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero); }
public static void SetAssociation(string Extension, string KeyName, string FileDescription) { if (!requestPrivilege()) { return; } RegistryKey BaseKey; RegistryKey OpenMethod; RegistryKey Shell; RegistryKey CurrentUser; string OpenWith = Application.ExecutablePath; BaseKey = Registry.ClassesRoot.CreateSubKey(Extension); BaseKey.SetValue("", KeyName); OpenMethod = Registry.ClassesRoot.CreateSubKey(KeyName); OpenMethod.SetValue("", FileDescription); OpenMethod.CreateSubKey("DefaultIcon").SetValue("", "\"" + OpenWith + "\",0"); Shell = OpenMethod.CreateSubKey("Shell"); Shell.CreateSubKey("edit").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\""); Shell.CreateSubKey("open").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\""); BaseKey.Close(); OpenMethod.Close(); Shell.Close(); // Tell explorer the file association has been changed SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero); }
public void SetAssociation(string extension, string file = null) { RegistryKey BaseKey; RegistryKey OpenMethod; RegistryKey Shell; RenameSubKey(Registry.ClassesRoot, extension, extension + "_back"); SetStatus("SetAssociation:enter"); BaseKey = Registry.ClassesRoot.CreateSubKey(extension); BaseKey.SetValue("", _keyName); OpenMethod = Registry.ClassesRoot.CreateSubKey(_keyName); OpenMethod.SetValue("", _keyName);//_fileDescription); if (!string.IsNullOrEmpty(file)) { _openWith = file; } OpenMethod.CreateSubKey("DefaultIcon").SetValue("", "\"" + _openWith + "\",0"); Shell = OpenMethod.CreateSubKey("Shell"); Shell.CreateSubKey("edit").CreateSubKey("command").SetValue("", "\"" + _openWith + "\"" + " \"%1\""); Shell.CreateSubKey("open").CreateSubKey("command").SetValue("", "\"" + _openWith + "\"" + " \"%1\""); BaseKey.Close(); OpenMethod.Close(); Shell.Close(); // Tell explorer the file association has been changed SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero); }
//START -- Association | .gs public static void SetAssociation(string Extension, string KeyName, string OpenWith, string FileDescription) { if (!UacHelper.IsUacEnabled || !UacHelper.IsProcessElevated) { return; } RegistryKey BaseKey; RegistryKey OpenMethod; RegistryKey Shell; RegistryKey CurrentUser; BaseKey = Registry.ClassesRoot.CreateSubKey(Extension); BaseKey.SetValue("", KeyName); OpenMethod = Registry.ClassesRoot.CreateSubKey(KeyName); OpenMethod.SetValue("", FileDescription); OpenMethod.CreateSubKey("DefaultIcon").SetValue("", "\"" + OpenWith + "\",0"); Shell = OpenMethod.CreateSubKey("Shell"); Shell.CreateSubKey("edit").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\""); Shell.CreateSubKey("open").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\""); BaseKey.Close(); OpenMethod.Close(); Shell.Close(); CurrentUser = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + Extension, true); CurrentUser.DeleteSubKey("UserChoice", false); CurrentUser.Close(); SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero); }
public static void SetECLPFileOpenHandler() { RegistryKey BaseKey; RegistryKey OpenMethod; RegistryKey Shell; RegistryKey CurrentUser; RegistryKey hkcuClasses = Registry.CurrentUser.OpenSubKey(@"Software\Classes", true); string Extension = ".eclp"; string KeyName = "ECLProfile"; string OpenWith = System.Reflection.Assembly.GetEntryAssembly().Location; string FileDescription = "EVE Custom Launcher profile"; BaseKey = hkcuClasses.CreateSubKey(Extension); BaseKey.SetValue("", KeyName); OpenMethod = hkcuClasses.CreateSubKey(KeyName); OpenMethod.SetValue("", FileDescription); OpenMethod.CreateSubKey("DefaultIcon").SetValue("", "" + OpenWith + ",0"); Shell = OpenMethod.CreateSubKey("Shell"); Shell.CreateSubKey("open").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"/profile:%1\""); BaseKey.Close(); OpenMethod.Close(); Shell.Close(); CurrentUser = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + Extension, true); if (CurrentUser != null) { CurrentUser.DeleteSubKey("UserChoice", false); CurrentUser.Close(); } SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero); }
/* ----------------------------------------------------------------- */ /// /// Open /// /// <summary> /// Opens the specified source directory with the specified /// application. /// </summary> /// /// <param name="src">Path to open.</param> /// <param name="method">Method to open.</param> /// <param name="app">Path of the application.</param> /// /* ----------------------------------------------------------------- */ public static void Open(this Entity src, OpenMethod method, string app) { if (!method.HasFlag(OpenMethod.Open)) { return; } var dest = src.IsDirectory ? src.FullName : src.DirectoryName; var skip = method.HasFlag(OpenMethod.SkipDesktop) && dest.FuzzyEquals(Environment.SpecialFolder.Desktop.GetName()); if (skip) { return; } var cvt = app.HasValue() ? app : "explorer.exe"; var klass = typeof(EntityExtension); klass.LogDebug($"Path:{src.FullName.Quote()}", $"App:{cvt.Quote()}"); klass.LogWarn(() => new Process { StartInfo = new() { FileName = cvt, Arguments = dest.Quote(), CreateNoWindow = false, UseShellExecute = true, LoadUserProfile = false, WindowStyle = ProcessWindowStyle.Normal, } }.Start());
public static void SetAssociation(string Extension, string KeyName, string OpenWith, string FileDescription) { RegistryKey BaseKey; RegistryKey OpenMethod; RegistryKey Shell; RegistryKey CurrentUser; BaseKey = Registry.ClassesRoot.CreateSubKey(Extension); BaseKey.SetValue("", KeyName); OpenMethod = Registry.ClassesRoot.CreateSubKey(KeyName); OpenMethod.SetValue("", FileDescription); OpenMethod.CreateSubKey("DefaultIcon").SetValue("", "\"" + OpenWith + "\",0"); Shell = OpenMethod.CreateSubKey("Shell"); Shell.CreateSubKey("edit").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\""); Shell.CreateSubKey("open").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\""); BaseKey.Close(); OpenMethod.Close(); Shell.Close(); CurrentUser = Registry.CurrentUser.CreateSubKey($@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\{Extension}"); CurrentUser = CurrentUser.OpenSubKey("UserChoice", RegistryKeyPermissionCheck.ReadWriteSubTree, System.Security.AccessControl.RegistryRights.FullControl); CurrentUser.SetValue("Progid", KeyName, RegistryValueKind.String); CurrentUser.Close(); // Delete the key instead of trying to change it CurrentUser = Registry.CurrentUser.OpenSubKey($"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\{Extension}", true); CurrentUser.DeleteSubKey("UserChoice", false); CurrentUser.Close(); // Tell explorer the file association has been changed SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero); }
//Thanks StackOverflow https://stackoverflow.com/questions/2681878/associate-file-extension-with-application public static void SetAssociation(string Extension, string KeyName, string OpenWith, string FileDescription) { RegistryKey BaseKey; RegistryKey OpenMethod; RegistryKey Shell; RegistryKey CurrentUser; BaseKey = Registry.ClassesRoot.CreateSubKey(Extension); BaseKey.SetValue("", KeyName); OpenMethod = Registry.ClassesRoot.CreateSubKey(KeyName); OpenMethod.SetValue("", FileDescription); OpenMethod.CreateSubKey("DefaultIcon").SetValue("", "\"" + OpenWith + "\",0"); Shell = OpenMethod.CreateSubKey("Shell"); Shell.CreateSubKey("edit").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\""); Shell.CreateSubKey("open").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\""); BaseKey.Close(); OpenMethod.Close(); Shell.Close(); CurrentUser = Registry.CurrentUser.CreateSubKey(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" + Extension); CurrentUser.DeleteSubKey("UserChoice", false); CurrentUser.SetValue("Progid", KeyName, RegistryValueKind.String); CurrentUser.Close(); //Notify Explorer SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero); }
private static bool AssociateBA2Extension() { try { RegistryKey BaseKey; RegistryKey OpenMethod; RegistryKey Shell; BaseKey = Registry.ClassesRoot.CreateSubKey(associateExtension); BaseKey.SetValue("", associateKeyName); OpenMethod = Registry.ClassesRoot.CreateSubKey(associateKeyName); OpenMethod.SetValue("", associateFriendlyName); OpenMethod.CreateSubKey("DefaultIcon").SetValue("", "\"" + associateExePath + "\",0"); Shell = OpenMethod.CreateSubKey("Shell"); Shell.CreateSubKey("edit").CreateSubKey("command").SetValue("", "\"" + associateExePath + "\"" + " \"%1\""); Shell.CreateSubKey("open").CreateSubKey("command").SetValue("", "\"" + associateExePath + "\"" + " \"%1\""); BaseKey.Close(); OpenMethod.Close(); Shell.Close(); // Tell explorer the file association has been changed NativeMethods.SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero); return(true); } catch (SecurityException) { return(false); } }
public static void SetWeakFileAssociation(string Extension, string KeyName, string OpenWith, string FileDescription, bool Unset = false) { RegistryKey BaseKey; RegistryKey OpenMethod; RegistryKey Shell; BaseKey = Registry.CurrentUser.CreateSubKey("Software\\Classes\\" + Extension, RegistryKeyPermissionCheck.ReadWriteSubTree); if (!Unset) { BaseKey.CreateSubKey("OpenWithProgids").SetValue(KeyName, ""); OpenMethod = Registry.CurrentUser.CreateSubKey("Software\\Classes\\" + KeyName); OpenMethod.SetValue("", FileDescription); Shell = OpenMethod.CreateSubKey("Shell"); Shell.CreateSubKey("open").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\""); OpenMethod.Close(); Shell.Close(); } else { RegistryKey ProgIds = BaseKey.OpenSubKey("OpenWithProgids", true); if (ProgIds != null) { ProgIds.DeleteValue(KeyName, false); } Registry.CurrentUser.OpenSubKey("Software\\Classes\\", true).DeleteSubKeyTree(KeyName, false); ProgIds.Close(); } BaseKey.Close(); // Tell explorer the file association has been changed SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero); }
public static bool Associated(string KeyName = "ByteFlood", string Description = "TORRENT File", string Extension = ".torrent") { RegistryKey BaseKey; RegistryKey OpenMethod; RegistryKey Shell; BaseKey = Registry.CurrentUser.OpenSubKey("Software", true).OpenSubKey("Classes", true); if (!BaseKey.GetSubKeyNames().Contains(Extension)) { return(false); } BaseKey = BaseKey.OpenSubKey(Extension); OpenMethod = Registry.CurrentUser.OpenSubKey("Software", true).OpenSubKey("Classes", true); if (!OpenMethod.GetSubKeyNames().Contains(KeyName)) { return(false); } OpenMethod = OpenMethod.OpenSubKey(KeyName); Shell = OpenMethod.OpenSubKey("Shell"); if (!Shell.GetSubKeyNames().Contains("open")) { return(false); } BaseKey.Close(); OpenMethod.Close(); Shell.Close(); return(true); }
public static void SetAssociation(string KeyName = "ByteFlood", string Description = "TORRENT File", string Extension = ".torrent") { RegistryKey BaseKey; RegistryKey OpenMethod; RegistryKey Shell; RegistryKey CurrentUser; BaseKey = Registry.CurrentUser.OpenSubKey("Software\\Classes", true).CreateSubKey(Extension); BaseKey.SetValue("", KeyName); OpenMethod = Registry.CurrentUser.OpenSubKey("Software\\Classes", true).CreateSubKey(KeyName); OpenMethod.SetValue("", Description); OpenMethod.CreateSubKey("DefaultIcon").SetValue("", "\"" + OpenWith + "\",0"); Shell = OpenMethod.CreateSubKey("Shell"); Shell.CreateSubKey("open").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\""); BaseKey.Close(); OpenMethod.Close(); Shell.Close(); CurrentUser = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\.torrent", true); CurrentUser.DeleteSubKey("UserChoice", false); CurrentUser.Close(); SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero); }
bool SetAssociation(string Extension, string KeyName, string OpenWith, string FileDescription) { RegistryKey BaseKey; RegistryKey OpenMethod; RegistryKey Shell; RegistryKey CurrentUser; try { BaseKey = Registry.ClassesRoot.CreateSubKey(Extension); BaseKey.SetValue("", KeyName); BaseKey.Flush(); BaseKey.Close(); OpenMethod = Registry.ClassesRoot.CreateSubKey(KeyName); OpenMethod.SetValue("", FileDescription); OpenMethod.CreateSubKey("DefaultIcon").SetValue("", "\"" + OpenWith + "\",0"); OpenMethod.Flush(); Shell = OpenMethod.CreateSubKey("Shell"); Shell.CreateSubKey("open").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\""); Shell.CreateSubKey("edit").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\""); Shell.Flush(); Shell.Close(); OpenMethod.Close(); // Delete the key instead of trying to change it CurrentUser = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + Extension, true); if (CurrentUser != null) { CurrentUser.DeleteSubKey("UserChoice", false); CurrentUser.Flush(); CurrentUser.Close(); } } catch (Exception ex) { if (ex is System.Security.SecurityException || ex is UnauthorizedAccessException) { MessageBox.Show("Please restart this program with administrative privileges to set file associations!"); return(false); } #if DEBUG MessageBox.Show(ex.ToString()); #else MessageBox.Show(ex.Message); #endif return(false); } // Tell explorer the file association has been changed SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero); return(true); }
public static void SetAssociation(string Extension, string KeyName, string OpenWith, string OpenWithParameter, string IconIndex, string FileDescription) { try { RegistryKey key = Registry.ClassesRoot.OpenSubKey(Extension); string temp1 = key.GetValue(null, "").ToString(); if (temp1.Trim().Length > 0) { Registry.ClassesRoot.DeleteSubKeyTree(temp1); } Registry.ClassesRoot.DeleteSubKeyTree(Extension); } catch { } RegistryKey BaseKey; RegistryKey OpenMethod; RegistryKey Shell; RegistryKey CurrentUser; BaseKey = Registry.ClassesRoot.CreateSubKey(Extension); BaseKey.SetValue("", KeyName); OpenMethod = Registry.ClassesRoot.CreateSubKey(KeyName); OpenMethod.SetValue("", FileDescription); OpenMethod.CreateSubKey("DefaultIcon").SetValue("", "\"" + OpenWith + "\"," + IconIndex); Shell = OpenMethod.CreateSubKey("Shell"); Shell.CreateSubKey("edit").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"" + OpenWithParameter + "\" \"%1\""); Shell.CreateSubKey("open").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"" + OpenWithParameter + "\" \"%1\""); BaseKey.Close(); OpenMethod.Close(); Shell.Close(); try { CurrentUser = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + Extension, true); CurrentUser.DeleteSubKey("UserChoice", false); CurrentUser.Close(); } catch { } // Tell explorer the file association has been changed SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero); }
public static void SetAssociation(string Extension, string KeyName, string OpenWith, string FileDescription) { try { RegistryKey CurrentUser; // The stuff that was above here is basically the same RegistryKey BaseKey; RegistryKey OpenMethod; RegistryKey Shell; BaseKey = Registry.ClassesRoot.CreateSubKey(Extension); BaseKey.SetValue("", KeyName); OpenMethod = Registry.ClassesRoot.CreateSubKey(KeyName); OpenMethod.SetValue("", FileDescription); OpenMethod.CreateSubKey("DefaultIcon").SetValue("", Application.StartupPath + "\\bcicons.dll" + ",0"); Shell = OpenMethod.CreateSubKey("Shell"); Shell.CreateSubKey("edit").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\""); Shell.CreateSubKey("open").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\""); BaseKey.Close(); OpenMethod.Close(); Shell.Close(); // Delete the key instead of trying to change it CurrentUser = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + Extension, true); CurrentUser.DeleteSubKey("UserChoice", false); CurrentUser.Close(); // Tell explorer the file association has been changed SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero); MessageBox.Show("Registry File Association Done", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show("Registry File Association Error" + Environment.NewLine + ex.Message + ex.Source, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void SetAssociation(string Extension, string KeyName, string OpenWith, string FileDescription) { RegistryKey BaseKey; RegistryKey OpenMethod; RegistryKey Shell; RegistryKey root = Registry.CurrentUser.CreateSubKey("Software"); root = root.CreateSubKey("Classes"); BaseKey = root.CreateSubKey(Extension); BaseKey.SetValue("", KeyName); OpenMethod = root.CreateSubKey(KeyName); OpenMethod.SetValue("", FileDescription); OpenMethod.CreateSubKey("DefaultIcon").SetValue("", "\"" + OpenWith + "\",0"); Shell = OpenMethod.CreateSubKey("Shell"); Shell.CreateSubKey("edit").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\""); Shell.CreateSubKey("open").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\""); BaseKey.Close(); OpenMethod.Close(); Shell.Close(); }
/// <summary> /// /// </summary> /// <param name="type">类型</param> /// <param name="showName">名称</param> /// <param name="xl">序号</param> /// <param name="p">字体偏移量</param> public void drawItem(string type, string showName, int xl, int p) { if (xl < 1) { xl = 1; } Image image = null; OpenMethod openMethod = null; if (type == "radminInstallAndActive") { openMethod = new OpenMethod(radminInstallAndActive); image = this.imageList.Images["radmin.png"]; } else if (type == "systemUserSafePwd") { openMethod = new OpenMethod(systemUserSafePwd); image = this.imageList.Images["usersafe.png"]; } else if (type == "cancelScreenLockPassword") { openMethod = new OpenMethod(cancelScreenLockPassword); image = this.imageList.Images["unlocksystem.png"]; } else if (type == "lockScreen") { openMethod = new OpenMethod(lockScreen); image = this.imageList.Images["locksystem.png"]; } else if (type == "clearCDisk") { openMethod = new OpenMethod(clearCDisk); image = this.imageList.Images["clearCDisk.png"]; } else if (type == "winUpdate") { openMethod = new OpenMethod(winUpdate); image = this.imageList.Images["winUpdate.png"]; } else if (type == "printShare") { openMethod = new OpenMethod(printShare); image = this.imageList.Images["printShare.png"]; } PictureBox p1 = new PictureBox(); p1.Image = image; p1.Parent = this.groupbox; p1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom; p1.Location = new System.Drawing.Point(24, 20 + (xl - 1) * 70); p1.Size = new System.Drawing.Size(36, 36); p1.Name = "picBox" + "_" + xl; p1.Click += new System.EventHandler((sender, e) => { if (isRunning) { plugFrm.Show(); return; } isRunning = true; plugFrm = new PlugFrm(); this.refreshUI += plugFrm.refreshUI; plugFrm.Show(); autoRunThread = new Thread(new ThreadStart(openMethod)); autoRunThread.Start(); }); Label label = new Label(); label.AutoSize = true; label.Font = new System.Drawing.Font("微软雅黑", 7, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); label.ForeColor = System.Drawing.Color.Black; label.Text = showName; label.Location = new System.Drawing.Point(2 + p, 20 + (xl - 1) * 70 + 36); label.Parent = this.groupbox; label.Name = "lab" + "_" + xl; //label.DoubleClick += new System.EventHandler((sender, e) => { // openMethod(urlDic[type],user,pwd); //}); xl++; }