Ошибка в коде автозагрузчика "В экземпляре объекта не задана ссылка на объект" - C#
Формулировка задачи:
Ошибка кода Автозагрузки С#
класс Autorun.cs
используеться в Form1.cs
происходит ошибка
возможно из за винды
Windows 8.1 Ultimatium
вот фото что выводит
public static bool SetAutorunValue(bool autorun, string name)
{
string ExePath = System.Windows.Forms.Application.ExecutablePath;
RegistryKey reg;
//reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\", true);
reg = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run\\");
try
{
if (autorun)
reg.SetValue(name, ExePath);
else
reg.DeleteValue(name);
reg.Close();
}
catch
{
return false;
}
return true;
}Autorun.SetAutorunValue(true, "UPG");
Решение задачи: «Ошибка в коде автозагрузчика "В экземпляре объекта не задана ссылка на объект"»
textual
Листинг программы
public static bool SetAutorunValue(bool autorun, string name)
{
string ExePath = System.Windows.Forms.Application.ExecutablePath;
RegistryKey reg=null;
reg = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run\\", autorun);
//reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\", true);
//reg = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run\\");
try
{
if (autorun)
reg.SetValue(name, ExePath);
else
reg.DeleteValue(name);
reg.Close();
}
catch
{
return false;
}
return true;
}