Search code examples
c#winformsargumentsregistry

Get argument from registry C#


I'm set key value in registry as follows-

 RegistryKey registryKey = Registry.CurrentUser.OpenSubKey
                ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);


  registryKey.SetValue("MobiCheckerTest", Application.ExecutablePath + "%autostart");

Registry looks like-

C:\Users\skpaul\Desktop\StartupApp\StartupApp\bin\Debug\StartupApp.EXE%autostart

In Program.cs

 static void Main(string[] args) //args= string[0].
    {
        Program.LaunchedViaStartup = args != null && args.Any(arg => arg.Equals("autostart", StringComparison.CurrentCultureIgnoreCase));

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

Form1.cs

    public Form1()
    {
        InitializeComponent();
        MessageBox.Show(this, string.Format("Lanched Via Startup Arg:{0}", Program.LaunchedViaStartup));
    }

Argument can not be read in program.cs.

Any help?


Solution

  • Why are you using a percent (%) sign? Try a [space] instead:

    registryKey.SetValue("MobiCheckerTest", "\"" + Application.ExecutablePath + "\" autostart");