Weird behavior with windows startup C#

Posted by FrieK on Stack Overflow See other posts from Stack Overflow or by FrieK
Published on 2010-05-25T19:37:43Z Indexed on 2010/05/25 19:41 UTC
Read the original article Hit count: 232

Filed under:
|
|
|

Hi, I've created an application with the option to start on Windows startup. First I did this via the Registry, like this:

private void RunOnStartup(bool RunOnStartup) {
    Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
    if (RunOnStartup) {
        key.SetValue(ProgramTitle, System.Windows.Forms.Application.ExecutablePath.ToString());
    } else {
        key.DeleteValue(ProgramTitle, false);
    }
}

And this worked, but not correctly. It started the .exe but with the same behavior as it was a new one with the default 'config.xml' it needs. Which is obviously wrong.

I did not manage to find out what was wrong, so I tried it differently: create a shortcut in the Startup folder. Couldn't go wrong I figured, I mean, it's just a shortcut right?

I used this code:

private void RunOnStartup(bool RunOnStartup) {
    string startup = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\"+ProgramTitle+".url";
    if (RunOnStartup) {
        using (StreamWriter writer = new StreamWriter(startup)) {
            string app = System.Reflection.Assembly.GetExecutingAssembly().Location;
            writer.WriteLine("[InternetShortcut]");
            writer.WriteLine("URL=file:///" + app);
            writer.WriteLine("IconIndex=0");
            string icon = app.Replace('\\', '/');
            writer.WriteLine("IconFile=" + icon);
            writer.Flush();
        }
    } else {
        if (File.Exists(startup)) {
            File.Delete(startup);
        }
    }
}

And this worked as well, it started, but with the same behavior.

So my question is, does anyone have any idea how this happens? Any help is much appreciated!

Thanks

© Stack Overflow or respective owner

Related posts about c#

Related posts about Windows