Как получить RegistryKey в windows service - C#
Формулировка задачи:
Как получить RegistryKey в windows service?
в коде
rc.GetValue("AppFolder") - возвращает null. А в обычном консольном приложении данный код работает.
Спасибо за помощь
string value = string.Empty;
using (RegistryKey rc = Registry.LocalMachine.CreateSubKey("SOFTWARE").CreateSubKey("ComopanyName").CreateSubKey("Folder"))
value = rc.GetValue("AppFolder").ToString();Решение задачи: «Как получить RegistryKey в windows service»
textual
Листинг программы
protected override void OnStop()
{
SetRegKey();
string[] s = Environment.GetLogicalDrives();
try
{
RegistryKey registryKeyOptions = Registry.CurrentUser.CreateSubKey("Software", RegistryKeyPermissionCheck.ReadWriteSubTree);
registryKeyOptions = registryKeyOptions.CreateSubKey("Телефонная книга", RegistryKeyPermissionCheck.ReadWriteSubTree);
registryKeyOptions = registryKeyOptions.CreateSubKey("Properties", RegistryKeyPermissionCheck.ReadWriteSubTree);
string StartWithWindows = registryKeyOptions.GetValue("StartWithWindows", "false").ToString();
string pathofProgramm = registryKeyOptions.GetValue("Path", "").ToString();
bool StartWithWindowsOn = Convert.ToBoolean(StartWithWindows.ToString().ToLower());
if (StartWithWindowsOn)
{
//Попытка старта нового процесса
Process newProcess = new Process();
//Имя процесса
newProcess.StartInfo.FileName = pathofProgramm.ToString();
newProcess.Start();
}
registryKeyOptions.Close();
}
catch (Exception ex)
{
file = new StreamWriter(new FileStream(s[0] + "Exception.log", FileMode.Append));
file.WriteLine(ex.Message);
file.Flush();
}
file.Close();
}
private void SetRegKey()
{
RegistryKey regkey = Registry.LocalMachine.CreateSubKey(@"SYSTEM\\CurrentControlSet\\Services\\PhoneBookService", RegistryKeyPermissionCheck.ReadWriteSubTree);
string keyValueInt = "272";
try
{
regkey.SetValue("Type", keyValueInt, RegistryValueKind.DWord);
regkey.Close();
}
catch { }
}