Skip to main content
 首页 » 编程设计

c#之如何在启动时向应用程序提供参数

2024年02月13日15jillzhang

我一直想知道如何在启动时向应用程序提供命令行参数。
我的意思是在代码中输入什么,以便创建一个提供命令行参数的注册表项:- 我一直在使用以下代码来创建一个注册表项(以便应用程序在启动时启动。

    using Microsoft.Win32; 
    private void SetStartup() 
    { 
        RegistryKey rk = Registry.CurrentUser.OpenSubKey 
        ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); 
         rk.SetValue(AppName, Application.ExecutablePath.ToString());            
 
} 

谁能告诉我应该修改什么参数

    rk.SetValue()  

函数的参数为​​我的应用程序提供命令行参数。

请您参考如下方法:

如果我理解正确,您希望在应用程序在 Windows 启动时添加命令行参数?

那么你必须做类似的事情:

string cmd = Application.ExecutablePath.ToString() + " /arg1 /arg2 /arg3 ....."; 
RegistryKey rk = Registry.CurrentUser.OpenSubKey 
        ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); 
         rk.SetValue(AppName, cmd);