开发者

System.Data.SQLite and strange File.Copy errors

I find that File.Copy will gladly copy a file onto itself without throwing an exception. By mistake I copied a sqlite database file onto itself, and the file is no longer a valid database. I have repeated this as a test several times and get the same result. Using winmerge (or a text editor) I see that the result file has 0 bytes, but windows explorer still shows the original file size. What have I missed here? (vs2008, win7-64)

Edit: some code. Pretty simplistic I know. And it's not 0 bytes, it's all nuls.

        public bool RestoreDatabaseSqlite(string backupFilePath)
    {
        try
        {
            File.Copy(backupFilePath, _databaseFilePath, true);
        }
        catch(Exception ex)
        {
            MessageBox.Show("Error restoring database file: " + ex.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }
开发者_如何转开发
        return true;
    }

Edit #2: The problem is specific to System.Data.SQLite, an open SQLiteConnection on the file, and the interaction with File.Copy. I would expect the file copy to fail with an exception, but it just silently nuls out the entire file.


Quick test:

File.Copy("foo.htm", "foo.htm");

throws an IOException:

The file 'foo.htm' already exists.

Trying with overwrite:

File.Copy("foo.htm", "foo.htm", true);

throws an IOException:

The process cannot access the file 'foo.htm' because it is being used by another process.

So generally it looks to be covered. Are you doing something specific?


I added some additional double-checking, and it still seems fine:

Console.WriteLine("Before:");
Console.Write(File.ReadAllText("foo.htm"));
Console.WriteLine();
Console.WriteLine("After:");
try
{
    File.Copy("foo.htm", "foo.htm", true);
}
catch (IOException) {
    Console.Write(File.ReadAllText("foo.htm"));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜