Windows启动后,应用程序无法找到设置或图像文件

问题描述 投票:1回答:1

我有一个应用程序,在Windows启动后启动。这很好,但应用程序似乎无法找到必要的csv文件与设置或导致崩溃的必要资源。但是,如果我在登录Windows帐户后手动打开它,应用程序工作正常。

我试图检查csv文件是否真的存在,但如上所述,应用程序找不到它。由于csv文件包含一些设置和语言数据,因此如果找不到该文件,则无法运行该应用程序

在我读出csv文件之前,我通过执行以下操作检查文件是否存在:

     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);
    }

此外,我认为重要的是不要注意以下内容:此错误仅在计算机关闭至少几个小时后才会发生。重启或关闭电脑并在几分钟后重新打开它不会给我错误。

我该怎么做才能改善这一点?为什么它会在自动启动时崩溃而不是在我手动启动时崩溃?

c# windows settings boot
1个回答
1
投票

这是因为当它自动加载时,工作目录与从.exe手动打开时不同,请尝试使用AppDomain.CurrentDomain.BaseDirectory确保路径是相对于您的应用程序的,也可以使用它来指定.csv文件路径,假设该文件与您的应用程序位于同一目录中:

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);
    }
© www.soinside.com 2019 - 2024. All rights reserved.