文件的还原与备份
1。 还原
private void menuOpen_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = ConfigurationManager.AppSettings["FilePath"] + "DataBase";
ofd.Title = "请选择需要打开的数据文件";
ofd.Filter = "xxx数据文件|*.xxx|所有文件|*.*"; //后面加上你需要打开的文件类型
ofd.CheckFileExists = true;
ofd.CheckPathExists = true;
ofd.Multiselect = false;
if (ofd.ShowDialog() == DialogResult.OK)
{
string basepath = GlobalParam.DataPath + @"DataBase\xxxxxx.xxx";
string renamepath = basepath.Substring(0, basepath.Length - 4) +
DateTime.Now.ToString("yyyyMMddHHmmss") + ".xxx";
//重命名文件
File.Move(basepath, renamepath);
//拷贝文件
File.Copy(ofd.FileName, basepath, true);
}
}
2。备份
private void menuBackup_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "保存";
sfd.Filter = "xxx数据文件|*.xxx";
sfd.CheckPathExists = true;
sfd.RestoreDirectory = true;
sfd.FileName = "xxxxxxx" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xxx";
if (sfd.ShowDialog() == DialogResult.OK)
{
string basepath = GlobalParam.DataPath + @"DataBase\xxxxxxx.xxx";
File.Copy(basepath, sfd.FileName, true);
}
}
浙公网安备 33010602011771号