views:

128

answers:

2

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

+1  A: 

I suspect your application is being launched with a working directory different from the directory the executable is in (by looking at my own process list, they have the user's profile as their working directory), and this is why your config.xml is not found. However, applications for which I have shortcuts (real shortcuts, i.e. .lnk files, not Internet shortcuts, i.e. .url files, like what you tried to do) have their working directory set to the directory specified in the shortcut.

To create a shell link (.lnk) easily, you can try using the COM interfaces and classes exposed by shell32.dll, specifically ShellLinkObject and ShellLinkObjectClass. Make sure to set the WorkingDirectory property correctly!

Alternatively, change the program to make it alter its working directory on startup based on the executable path.

Francis Gagné
Thanks for the reply, I've been trying to create Ink files, but with every example I find it gives errors. When I manually make the Ink file (empty) and then run the code, it doesn't give any error, but the Ink isn't filled with data either.But what do you exactly mean with the WorkingDir being different than the ExecutedPath? How can it be different?
FrieK
A: 

Thank you very much Francis Gagné!

I've managed to create a working .lnk and it all works now :)

Code:

public static void CreateShortcut(string Filename, string InkLocation, string Description) {
            string TargetDirectory = "";
            string[] splitted = Filename.Split('\\');
            for (int i = 0; i < splitted.Length - 1; i++) {
                TargetDirectory += "\\" + splitted[i];
            }
            TargetDirectory = TargetDirectory.Substring(1);
            WshShellClass wsh_Shell = new WshShellClass();
            IWshShortcut myshorcut = wsh_Shell.CreateShortcut(InkLocation) as IWshShortcut;
            myshorcut.TargetPath = Filename;
            myshorcut.Description = Description;
            myshorcut.WorkingDirectory = TargetDirectory;
            myshorcut.Save();
        }
FrieK