Search code examples
c#windowssettingsboot

Application can't find settings or image files after windows boot


I have an application which boots after the windows boot. That works well, however the application can't seem to find the necessary csv file with settings or the necessary resources which result in a crash. However application works fine if I manually open it after I log in into my Windows account.

I tried to do a check if the csv file really exists, but as said above, the application can't find it. Since the csv file contains some settings and language data the application cannot run if the file isn't found

Before I read out the csv file, I check if the file exists by doing the following:

     try {
        string langFile = Path.Combine(Settings.Default.WorkingDirectory, "languageSupport.csv"); //Get language file
        // Read out the file
    } catch {
        MessageBox.Show("Could not load settings data!");
        Console.WriteLine("Error occured: " + e.Message);
    }

Also, I think it is important no note the following: This error only happens when the computer has been turned off for atleast a few hours. A reboot or shutting the pc down and turning it back on after a few minutes doesn't give me the error.

What can I do to improve this? Why does it crash when it boots automatically and not when I boot it manually?


Solution

  • It is because when it load automatically the working directory is different than when you open it manually from the .exe, try using AppDomain.CurrentDomain.BaseDirectory to make sure that paths are relative to your application, also you can use it to specify the .csv file path, assuming that the file is in the same directory as your application:

    try {
            string langFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "languageSupport.csv"); //Get language file
            // Read out the file
        } catch {
            MessageBox.Show("Could not load settings data!");
            Console.WriteLine("Error occured: " + e.Message);
        }