C#数据备份与还原,源码实例

首次发布:2018-01-26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
private void button1_Click(object sender, EventArgs e)
{
    FolderBrowserDialog fbd = new FolderBrowserDialog();
    if (fbd.ShowDialog() == DialogResult.OK && !string.IsNullOrEmpty(fbd.SelectedPath))
    {
        this.txtBackup.Text = fbd.SelectedPath;
    }
}
 
private void btnBackup_Click(object sender, EventArgs e)
{
    if (txtBackup.Text == "")
    {
        MessageBox.Show("请选择要备份的目录");
        return;
    }
    string backupData = txtBackup.Text +"\\"+ DateTime.Now.ToString("yyyyMMddHHmmss") + "Data.db";
    string sourcePath = AppDomain.CurrentDomain.BaseDirectory + "DataBase\\Data.db"; //源文件完整路径
    File.Copy(sourcePath, backupData,true);
    if (File.Exists(backupData))
    {
        MessageBox.Show("备份成功");
    }
    else
    {
        MessageBox.Show("备份失败");
    }
}
 
private void button2_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.DefaultExt = "db";
    ofd.Filter = "数据库文件(*.db)|*.db";
    ofd.Multiselect = false;
    if (ofd.ShowDialog() == DialogResult.OK && !string.IsNullOrEmpty(ofd.FileName))
    {
        this.txtReduction.Text = ofd.FileName;
    }
}
 
private void btnReduction_Click(object sender, EventArgs e)
{
    if (txtReduction.Text == "")
    {
        MessageBox.Show("请选择要还原的文件");
        return;
    }
    string sourcePath = AppDomain.CurrentDomain.BaseDirectory + "DataBase\\Data.db"; //源文件完整路径
    File.Copy(txtReduction.Text, sourcePath, true);
    if (File.Exists(sourcePath))
    {
        MessageBox.Show("备份成功");
    }
    else
    {
        MessageBox.Show("备份失败");
    }
}