在windows启动时安装c#windows应用程序

在windows启动时安装c#windows应用程序,c#,winforms,c#-4.0,c#-3.0,c#-2.0,C#,Winforms,C# 4.0,C# 3.0,C# 2.0,我正在编写一个windows应用程序,希望创建不同的对话框(是/否类型)等。但我希望每次windows启动时都运行此应用程序。但一旦下次用户登录windows时安装此应用程序,将在后台进程中自动启动。My(ScreenDialog.exe)在C:\目录中找到,应用程序需要管理员权限才能使用C目录。 但我使用的代码不起作用 RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\Current

我正在编写一个windows应用程序,希望创建不同的对话框(是/否类型)等。但我希望每次windows启动时都运行此应用程序。但一旦下次用户登录windows时安装此应用程序,将在后台进程中自动启动。My(ScreenDialog.exe)在C:\目录中找到,应用程序需要管理员权限才能使用C目录。 但我使用的代码不起作用

RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
key.SetValue("ScreenDialog", "C:\\ScreenDialog.exe");

我使用自己的类,首先检查如下:

  • Windows版本
  • 以管理员身份运行程序
根据上述选项,您可以在注册表*中找到所需的文件夹,如果该文件夹未存储在注册表中或出现错误,则必须在windows启动文件夹中创建程序的**快捷方式

我的班级代码:

使用系统;
使用System.IO;
使用System.Windows.Forms;
使用System.Runtime.InteropServices;
使用System.Security.Principal;
使用Microsoft.Win32;
公共静态类组件控制器
{
/// 
///应用程序是否以管理员身份运行?
/// 
公共静态布尔值IsAdmin()
{
WindowsIdentity=WindowsIdentity.GetCurrent();
如果(标识!=null)
return(新WindowsPrincipal(identity)).IsInRole(WindowsBuiltInRole.Administrator);
返回false;
}
公共静态布尔值IsWindowsVistaOrHigher()
{
OperatingSystem os=Environment.OSVersion;
返回((os.PlatformID.Win32NT)=&(os.Version.Major>=6));
}
/// 
///将此应用的可执行文件添加到注册表启动路径:
///'LocalMachine\SOFTWARE\Microsoft\Windows\CurrentVersion\Run'
/// 
///以管理员身份运行
公共静态void AddToStartup(布尔targetEveryone)
{
尝试
{
使用(RegistryKey main=(targetEveryone&IsAdmin()?Registry.LocalMachine:Registry.CurrentUser))
{
使用(RegistryKey=main.OpenSubKey(“软件\\Microsoft\\Windows\\CurrentVersion\\Run”,true))
{
String fileName=Path.GetFileNameWithoutExtension(Application.ExecutablePath);
if(key.GetValue(fileName)==null)
key.SetValue(文件名,Application.ExecutablePath);
}
}
}
捕获(例外情况除外)
{
//报告异常。。。
//
//将快捷方式复制到CommonStartUp或StartUp
//
尝试
{
Environment.SpecialFolder=((targetEveryone&&IsWindowsVistaOrHigher())?Environment.SpecialFolder.CommonStartup:Environment.SpecialFolder.Startup);
字符串fileDestination=Path.Combine(Environment.GetFolderPath(folder),Path.GetFileNameWithoutExtension(Application.ExecutablePath))+“.lnk”;
如果(!File.Exists(fileDestination))
创建(fileDestination,Application.ExecutablePath,null,null,“description…”,null,null);
}
捕获(异常扩展)
{
//报告异常。。。
}
}
}
公共类快捷方式
{
私有静态类型m_Type=Type.GetTypeFromProgID(“WScript.Shell”);
私有静态对象m_shell=Activator.CreateInstance(m_类型);
[ComImport,TypeLibType((短)0x1040),Guid(“F935DC23-1CF0-11D0-ADB9-00C04FD58A0B”)]
专用接口快捷方式
{
[DispId(0)]
字符串全名{[return:marshallas(UnmanagedType.BStr)][DispId(0)]get;}
[DispId(0x3e8)]
字符串参数{[return:marshallas(UnmanagedType.BStr)][DispId(0x3e8)]get;[param:In,marshallas(UnmanagedType.BStr)][DispId(0x3e8)]set;}
[DispId(0x3e9)]
字符串说明{[return:marshallas(UnmanagedType.BStr)][DispId(0x3e9)]get;[param:In,marshallas(UnmanagedType.BStr)][DispId(0x3e9)]set;}
[DispId(0x3ea)]
字符串热键{[return:marshallas(UnmanagedType.BStr)][DispId(0x3ea)]get;[param:In,marshallas(UnmanagedType.BStr)][DispId(0x3ea)]set;}
[DispId(0x3eb)]
字符串IconLocation{[return:marshallas(UnmanagedType.BStr)][DispId(0x3eb)]get;[param:In,marshallas(UnmanagedType.BStr)][DispId(0x3eb)]set;}
[DispId(0x3ec)]
字符串相对路径{[param:In,marshallas(UnmanagedType.BStr)][DispId(0x3ec)]集;}
[DispId(0x3ed)]
字符串TargetPath{[return:marshallas(UnmanagedType.BStr)][DispId(0x3ed)]get;[param:In,marshallas(UnmanagedType.BStr)][DispId(0x3ed)]set;}
[DispId(0x3ee)]
int WindowStyle{[DispId(0x3ee)]get;[param:In][DispId(0x3ee)]set;}
[DispId(0x3ef)]
字符串工作目录{[return:marshallas(UnmanagedType.BStr)][DispId(0x3ef)]get;[param:In,marshallas(UnmanagedType.BStr)][DispId(0x3ef)]set;}
[TypeLibFunc((短)0x40),DispId(0x7d0)]
无效加载([In,marshallas(UnmanagedType.BStr)]字符串路径链接);
[DispId(0x7d1)]
作废保存();
}
公共静态无效创建(字符串文件名、字符串目标路径、字符串参数、字符串工作目录、字符串描述、字符串热键、字符串iconPath)
{
IWshShortcut shortcut=(IWshShortcut)m_type.InvokeMember(“CreateShortcut”,System.Reflection.BindingFlags.InvokeMethod,null,m_shell,新对象[]{fileName});
快捷方式。描述=描述;
shortcut.TargetPath=TargetPath;
shortcut.WorkingDirectory=string.IsNullOrEmpty(WorkingDirectory)?targetPath:WorkingDirectory;
参数=参数;
如果(!string.IsNullOrEmpty(热键))快捷键。热键=热键;
如果(!string.IsNullOrEmpty(iconPath))shortcut.IConLocation