File.Exists始终返回false

时间:2020-03-02 13:44:31

标签: c#

我的应用程序是32位.Net VS 2017应用程序。 File.Exists在我的应用程序中始终返回false。无论是在VS中运行,在本地部署还是以管理员身份运行。在Windows 10 64位系统上运行。路径是好的,因为File.Copy可以正常工作(但是总是因为File.Exist无法正常工作。我不希望File.Copy运行,除非文件不存在。如果我找不到它,则可能是解决方法的建议可以正常工作吗?文件权限应该不是问题,因为该文件位于Documents文件夹中。也许比MyDocuments更好用的SpecialFolder?会有所帮助。在此先感谢。下面的代码。

// Class variables

public static string appPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + 
"\\Lottery Analyzer Expert International";  
public static string dbPath = appPath + "\\database\\";
public static string dbFile = "Histories.sqlite";

// Class method

public void copyInputFiles_db()
{    
    string dest = dbPath + dbFile;
    string src = Application.StartupPath + "\\database\\" + dbFile;

    if (!File.Exists(dest)) { }
    {
        File.Copy(src, dest, true);  // if input files not found in appPath copy from install folder

        bool do_download = true;
        DialogResult dialogResult2 = MessageBox.Show(
            "The history database was copied from the application's startup to it's working dirctory. This happens when first running " + 
            "the application or the history file is missing. Would you like to update that file from the web?", 
            "Download file?", MessageBoxButtons.YesNo);
        if (dialogResult2 == DialogResult.Yes)
        {
            do_download = true;
        }
        else if (dialogResult2 == DialogResult.No)
        {
            do_download = false;
        }

        if (do_download)
            downloadAllTheHistoryFIles_db();
    }
    printTextFiles();
}

1 个答案:

答案 0 :(得分:6)

您知道if下面的代码总是被执行吗?

if (!File.Exists(dest)) { } // << the { } is the IF scope   
{    // <- this is a new scope also, but not part of the if..

    File.Copy(src, dest, true);  
    bool do_download = true;
    // *SNIP*

}

移除{ }后面的if

相关问题