简体   繁体   English

在Windows上运行C#应用程序启动

[英]Run C# Application on Windows Start up

I'm trying to start a few applications for the current user only when Windows starts up. 我正在尝试仅在Windows启动时为当前用户启动一些应用程序。

I can accomplish this with the following: 我可以通过以下方式完成此任务:

RegistryKey oKey = Registry.CurrentUser.OpenSubKey("Software", true);
oKey = oKey.OpenSubKey("Microsoft", true);
oKey = oKey.OpenSubKey("Windows", true);
oKey = oKey.OpenSubKey("CurrentVersion", true);
oKey = oKey.OpenSubKey("Run", true);
oKey.SetValue("Application 1", "C:\\path\\to\\ap1.exe");
oKey.SetValue("Application 2", "C:\\path\\to\\ap2.exe");

But I'm trying to run this as part of a Visual Studio Installer Project. 但我正在尝试将其作为Visual Studio Installer项目的一部分运行。 I've added my custom action, the program starts like it should, and the installer works great in XP. 我添加了我的自定义操作,程序就像应该启动一样,安装程序在XP中运行良好。

In Windows 7, the installer gets elevated privileges, and does everything but insert the entries into the registry for the current user. 在Windows 7中,安装程序获得提升的权限,并执行所有操作,只是将条目插入到当前用户的注册表中。 How ever, it does insert the registry entries when ran as a standalone application (outside of the installer project) and it does not get elevated privileges. 但是,当作为独立应用程序(在安装程序项目之外)运行时,它确实插入了注册表项,并且它没有获得提升的权限。

The only thing I can figure is that with the elevated privileges, it's trying to insert the entries into the Administrators account instead of the current user? 我唯一可以想到的是,使用提升的权限,它试图将条目插入管理员帐户而不是当前用户? or is there something else I'm missing? 或者还有其他我想念的东西? and is there another way I can achieve my goal here? 还有另一种方法可以实现我的目标吗?

Is there a reason to not use the startup folder for the user? 是否有理由不为用户使用启动文件夹?

More than likely the problem is the user the installer is executing under. 问题很可能是安装程序正在执行的用户。 If the user isn't the admin the elevated installer will run under the context of the user who elevated the process. 如果用户不是管理员,则提升的安装程序将在提升进程的用户的上下文中运行。

It would be a safer choice to add your application to the startup folder or to add the registry key on first launch. 将应用程序添加到启动文件夹或在首次启动时添加注册表项是更安全的选择。

If the installer is getting elevated permissions, why write the setting to HKCU? 如果安装程序获得了提升权限,为什么要将设置写入HKCU? Write it to HKLM instead. 把它写给HKLM。 It will then take effect for all users. 然后它将对所有用户生效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM