private bool setAutoRunValue(bool autorun, string path) { const string AutoFileName = "Speech Recognition"; string ExePath = path; RegistryKey Reg; Reg = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run\\"); try { if (autorun) { Reg.SetValue(AutoFileName, ExePath); } else { Reg.DeleteValue(AutoFileName); } Reg.Flush(); Reg.Close(); } catch (Exception) { return(false); } return(true); }
/// <summary> /// 注册表操作,删除注册表中启动项 /// </summary> public static bool DeleteRegisterApp(string name) { try { if (!CheckExistRegisterApp(name)) { return(true); } Microsoft.Win32.RegistryKey Reg; Reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true); if (Reg == null) { Reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run"); } Reg.DeleteValue(name, false); Reg.Close(); } catch (Exception ex) { return(false); } return(true); }
/// <summary> /// 注册表操作,将程序添加到启动项 /// </summary> public static void SetRegistryApp(string name, string path) { try { if (CheckExistRegisterApp(name)) { return; } Microsoft.Win32.RegistryKey Reg; Reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true); if (Reg == null) { Reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run"); } Reg.SetValue(name, path); Reg.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public string FilePath(string ConfigFileName, string Name = "") { string path = ""; RegistryKey Reg; try { Reg = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64); Reg = Reg.OpenSubKey("SOFTWARE\\Exell\\" + Name); path = Reg.GetValue("Path").ToString(); Reg.Close(); if (File.Exists(path + "\\" + ConfigFileName) || File.Exists(path + ConfigFileName)) { if (path[path.Length - 1].ToString() == "\\") { return(path + ConfigFileName); } else { return(path + "\\" + ConfigFileName); } } } catch (Exception Ex) { //MessageBox.Show(Ex.Message); } if (File.Exists(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) + @"\Exell\" + Name + @"\" + ConfigFileName)) { return(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) + @"\Exell\" + Name + @"\" + ConfigFileName); } else if (File.Exists(ConfigFileName)) { return(Environment.CurrentDirectory + "\\" + ConfigFileName); } else { MessageBox.Show("Wskaż lokalizację pliku \"" + ConfigFileName + "\"."); OpenFileDialog OFP = new OpenFileDialog(); OFP.Filter = "*.ini|*.ini"; OFP.ShowDialog(); if (OFP.FileName == "") { /*if (MessageBox.Show("Proszę podać ścieżkę do pliku " + this.ConfigFileName + ". Prawdopodobnie znajduje się on w folderze z Co-Librem.", "", MessageBoxButtons.OKCancel) == DialogResult.OK) * FilePath(); * else * { * if (MessageBox.Show("Zamknąć?", "", MessageBoxButtons.YesNo) == DialogResult.Yes) * Application.Exit(); * }*/ } else { return(OFP.FileName); } return(""); } }