您的位置:首页 > 其它

文件备份与还原功能

2016-05-05 16:51 344 查看
         往往为了安全起见,用户需要备份很重要的文件,以免误操作,造成不必要的损失,为此在很多软件的设计过程中都开始要求有数据备份和恢复的功能,需要备份一些有用的文件到其他安全的存储介质中,尤其是数据库。那么在软件设计过程中如何实现文件的备份呢?那么我就以备份Access数据库为例,简单介绍一下该功能的实现。

文件备份         

''' <summary>
''' 系统备份
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click   '使用的事件为label的单击事件
Try
Dim TargetFileName As String     '目标文件名
Dim spath As String
Dim ssource As String
Dim se As DialogResult           '浏览文件对话框,要在窗体中添加FolderBrowserDialog控件

se = Me.FolderBrowserDialog1.ShowDialog()   '弹出浏览文件对话框

If se = DialogResult.OK Then            '如果选择了目标文件夹

TargetFileName = FolderBrowserDialog1.SelectedPath & "/HumanResourcesSystem.mdb"    '获取该文件夹路径

'获得文件路径最后一位是否含有/
If Microsoft.VisualBasic.Right(Application.StartupPath, 1) <> "/" Then

spath = Application.StartupPath & "/"
ssource = spath & "HumanResourcesSystem.mdb"      '系统数据库存放位置

If Dir$(TargetFileName) <> "" Then
If MsgBox("文件已存在,确认替换它!", vbYesNo + vbQuestion) = vbNo Then Exit Sub
Kill(TargetFileName)     '删除文件
FileCopy(ssource, TargetFileName)    '复制文件
'DBEngine.CompactDatabase ssource, TargetFileName    '压缩文件
MsgBox("数据备份成功!", vbInformation, "提示")
Else
FileCopy(ssource, TargetFileName)    '复制文件

MsgBox("数据备份成功!", vbInformation, "提示")

End If
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message.ToString())
End Try
End Sub


       该文件备份功能适用于所有文件的备份,对于备份路径的问题,大家可以根据需求更改获取目标文件夹和源文件的地址。同样可以实现备份。        

   文件还原    

       文件还原功能和备份功能的实现是类似的,都是使用的FileCopy。只不过路径要根据需要进行更改。下面通过源码和注解来进行区别和分析。
''' <summary>
''' 文件还原
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub Label8_Click(sender As Object, e As EventArgs) Handles Label8.Click       '需要使用openFileDialog控件
Try
Dim TargetFileName As String     '目标文件名
Dim spath As String
Dim ssource As String
Dim se As DialogResult           '返回选中文件的结果

OpenFileDialog1.Filter = "数据库文件|*.mdb"           '定义打开文件的格式
se = OpenFileDialog1.ShowDialog()

If se = DialogResult.OK Then                          '如果有文件已经选中

'MessageBox.Show("你选择的文件夹路径为:" & FolderBrowserDialog1.SelectedPath)
TargetFileName = OpenFileDialog1.FileName         '获取该文件的文件名(包括完整路径)

TargetFileName = TargetFileName.Replace("\HumanResourcesSystem.mdb", "/HumanResourcesSystem.mdb") '因为\代表的是路径,而/代表的是该文件。所以为了选择特定的文件,要将符号改为后边的。

If Microsoft.VisualBasic.Right(Application.StartupPath, 1) <> "/" Then

spath = Application.StartupPath & "/"
ssource = spath & "HumanResourcesSystem.mdb"

If Dir$(ssource) <> "" Then
If MsgBox("是否要还原数据,确认替换它!", vbYesNo + vbQuestion, "提示") = vbNo Then Exit Sub
Kill(ssource)     '删除文件
FileCopy(TargetFileName, ssource)    '复制文件

MsgBox("数据还原成功!", vbInformation, "提示")
Else
FileCopy(TargetFileName, ssource)    '复制文件
MsgBox("数据还原成功!", vbInformation, "提示")
End If
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message.ToString())
End Try
End Sub


         文件备份和还原都是相同的方法,只是使用的方式不同。所以,以后要将学习的知识活学活用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: