代码之家  ›  专栏  ›  技术社区  ›  Infiltrator

windows启动后应用程序找不到设置或映像文件

  •  0
  • Infiltrator  · 技术社区  · 7 年前

    我有一个在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);
        }
    

    另外,我认为不必注意以下几点:此错误仅在计算机关闭至少几个小时后发生。重新启动或关闭电脑并在几分钟后将其重新打开不会导致错误。

    我能做些什么来改进这个?为什么它会在自动引导时崩溃,而不是在我手动引导时崩溃?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Ali Ezzat Odeh    7 年前

    这是因为当它自动加载时,工作目录与从 .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);
        }
    
    推荐文章