Public Class Form1
Dim BackupPath As String = "C:\AhmedBackup\"
Dim DBFilename As String = "DATABASE.mdb"
Dim DBBackupFullFilename As String = String.Concat(BackupPath, "\", DBFilename)
Dim DBCurrentFullFilename As String = String.Concat(Application.StartupPath, "\", DBFilename)
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
If Not IO.Directory.Exists(BackupPath) Then IO.Directory.CreateDirectory(BackupPath)
If IO.File.Exists(DBBackupFullFilename) Then
If MsgBox( _
"الملف الحالي" & vbCrLf & DBCurrentFullFilename & vbCrLf & _
"الملف الجديد" & vbCrLf & DBBackupFullFilename & vbCrLf & _
"هل تريد استبدال الملف الحالي بالجديد ؟", _
MsgBoxStyle.Exclamation + MsgBoxStyle.MsgBoxRight + MsgBoxStyle.OkCancel, " نسخ ملف") = MsgBoxResult.Cancel _
Then
Exit Sub
End If
End If
If CompactDatabase(DBCurrentFullFilename, "123") Then
IO.File.Copy(DBCurrentFullFilename, DBBackupFullFilename, True)
MsgBox("تمت بنجاح عملية النسخ الاحتياطي")
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If IO.File.Exists(DBBackupFullFilename) Then
If MsgBox("هل تريد استبدال الملف الحالي بالاحتياطي ؟", MsgBoxStyle.Exclamation + MsgBoxStyle.MsgBoxRight + MsgBoxStyle.OkCancel, " استرجاع ملف") = MsgBoxResult.Cancel Then
Exit Sub
End If
End If
IO.File.Copy(DBBackupFullFilename, DBCurrentFullFilename, True)
MsgBox("تمت بنجاح عملية استرجاع الاحتياطي")
End Sub
Function CompactDatabase(ByVal dbFilename As String, Optional ByVal password As String = "") As Boolean
Try
Dim ext As String = IO.Path.GetExtension(dbFilename).ToLower
Dim provider As String
If ext = ".mdb" Then
provider = "Provider=Microsoft.Jet.OLEDB.4.0;"
ElseIf ext = ".accdb" Then
provider = "Provider=Microsoft.ACE.OLEDB.12.0;"
Else
MsgBox("File not 'mdb' or 'accdb'")
Return False
End If
Dim cpFilename As String = IO.Path.GetTempFileName & ".accdb"
Dim je As Object = CreateObject("JRO.JetEngine")
je.CompactDatabase( _
provider & ";Data Source=" & dbFilename & ";Jet OleDb:Database Password=" & password, _
provider & ";Data Source=" & cpFilename & ";Jet OleDb:Database Password=" & password & _
";Jet OLEDB:Engine Type=5")
IO.File.Delete(dbFilename)
IO.File.Move(cpFilename, dbFilename)
IO.File.Delete(cpFilename)
Return True
Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try
End Function
End Class