String baseDirectory = @"F:\Backup";
String mdbSourceFilePath = @"D:\Db\abc.mdb";
String mdbTargetFileName = "abc.mdb";
String fullDirectory = Path.Combine(baseDirectory, DateTime.Today.ToString("dddd"));
if (!Directory.Exists(baseDirectory))
Directory.CreateDirectory(baseDirectory);
if (!Directory.Exists(fullDirectory))
Directory.CreateDirectory(fullDirectory);
String mdbTargetFilePath = Path.Combine(fullDirectory, mdbTargetFileName);
File.Copy(mdbSourceFilePath, mdbTargetFilePath);
这是用于备份数据库。如果我单击备份按钮,则会创建备份数据库,但如果再次单击它,则不会覆盖现有数据库。我想替换现有的数据库。
答案 0 :(得分:0)
尝试检查该文件是否存在
if (File.Exists(fileName))
然后你可以使用
File.Replace
方法
答案 1 :(得分:0)
There is a third (optional) argument to File.Copy
called overwrite
. Set it to true
to overwrite an existing file, as long as it isn't read-only:
File.Copy(mdbSourceFilePath, mdbTargetFilePath, overwrite: true);
An alternative would be to check if mdbTargetFilePath
exists and delete it prior to the copy:
if (File.Exists(mdbTargetFilePath))
{
File.Delete(mdbTargetFilePath);
}