Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将我的应用程序登录到windows注册表?_C#_Windows_Registry - Fatal编程技术网

C# 如何将我的应用程序登录到windows注册表?

C# 如何将我的应用程序登录到windows注册表?,c#,windows,registry,C#,Windows,Registry,我正在编写一个应用程序,它需要在用户安装后每次启动计算机时都能工作。我在afterInstall事件中尝试在安装程序CALS上执行此操作,但安装程序将其自身放入注册表并在windows重新启动时运行,因此我尝试在Committed事件中执行此操作,并获得了相同的结果。之后,我将Committed property installer类设置为false,但Committed evet不会启动。我最后一次尝试是在安装后运行应用程序,然后让它将自己写入注册表,发生了一件奇怪的事情,它确实写入了注册表,

我正在编写一个应用程序,它需要在用户安装后每次启动计算机时都能工作。我在afterInstall事件中尝试在安装程序CALS上执行此操作,但安装程序将其自身放入注册表并在windows重新启动时运行,因此我尝试在Committed事件中执行此操作,并获得了相同的结果。之后,我将Committed property installer类设置为false,但Committed evet不会启动。我最后一次尝试是在安装后运行应用程序,然后让它将自己写入注册表,发生了一件奇怪的事情,它确实写入了注册表,但没有写入我想要的位置。有人知道为什么会这样,我如何修复它吗

我的代码:

bool registry = true;

RegistryKey rkSubKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", false);
string[] values = rkSubKey.GetValueNames();

foreach(string name in values)
{

  if (name.Equals("appName"))
    registry = false;
}

if (registry)
{

  RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
  rkApp.SetValue("appName", Application.ExecutablePath.ToString());
  DialogResult r = MessageBox.Show("The system now needs to restart your computer whould you like to do it now?", "Restart is needed", MessageBoxButtons.YesNo);
  if (r == DialogResult.Yes)
  {
    System.Diagnostics.Process.Start("ShutDown", "/r");
  }
  return;
}
mainModule.start();

您是否尝试过像这样打开subroot键smth:

var HKLM = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser,   Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32);
然后获取子密钥:

var baseKey = HKLM.OpenSubKey(...<the path here>..)
var baseKey=HKLM.OpenSubKey(…)

“没有我想要的地方”…这就引出了一个问题。。。那它到底写到哪里了?我不知道我查了整个注册表,找不到它…你在运行什么操作系统?64位还是32位?如果Vista/7/8运行的应用程序中有无UAC(用户帐户控制)?windows 7 32位我不知道什么是UAS isok我发现它把它放在注册表的所有用户部分它把它放在注册表的所有用户目录中但没问题谢谢:)但是它仍然写在所有用户部分而不是当前用户它写到HKEY\ U current\ U user\ \(在您的案例中,HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run)。您想将其写入何处?您也可以对该计算机上的当前用户使用LocalMachine配置单元。另外,请查看问题-它对该主题有非常有趣的建议。