PDA

View Full Version : سوال: کپی از یک فایل مشخص و ذخیره آن در محل دیگر با savefiledialog



karem2074
چهارشنبه 08 بهمن 1399, 12:10 عصر
با سلام دوستان
من در ریشه روت یه فایل دیتابیس اکسس دارم کلیک بک آپ براش گذاشتم یعنی میخوام از اون فایل اکسس دیتابیس که در مسیر date/Cantact.accdb قرار دارد اتوماتیک کپی بگیرد و با استفاده از پنجره savedialogfile هر کجا خواستم ذخیره کنه.
ممنون میشم کمک کنید


اینم کد که در ریشه اتوماتیک ذخیره میشه میخوام با استفاده از savedialofile بتونم هر جا بخوام ذخیره کنم.

Try
'set the destination and a file name with the date and time
Dim backupfiledestination As String = Application.StartupPath & "" & "backup_golden" & Format(Now(), "_yyyy-M-d_H-m-s") & ".accdb"
'location of the database file that you want to backup
Dim databaseFile As String = Application.StartupPath & "\data\Cantact.accdb"

'create a backup by using Filecopy Command to copy the file from location to destination
FileCopy(databaseFile, backupfiledestination)
MessageBox.Show("پشتیبان گیری با موفقیت انجام شد.", "پیغام پشتیبان گیری", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
'catch an error
MsgBox(ex.Message)

End Try

the king
چهارشنبه 08 بهمن 1399, 13:16 عصر
با سلام دوستان
من در ریشه روت یه فایل دیتابیس اکسس دارم کلیک بک آپ براش گذاشتم یعنی میخوام از اون فایل اکسس دیتابیس که در مسیر date/Cantact.accdb قرار دارد اتوماتیک کپی بگیرد و با استفاده از پنجره savedialogfile هر کجا خواستم ذخیره کنه.
ممنون میشم کمک کنید


اینم کد که در ریشه اتوماتیک ذخیره میشه میخوام با استفاده از savedialofile بتونم هر جا بخوام ذخیره کنم.

Try
'set the destination and a file name with the date and time
Dim backupfiledestination As String = Application.StartupPath & "" & "backup_golden" & Format(Now(), "_yyyy-M-d_H-m-s") & ".accdb"
'location of the database file that you want to backup
Dim databaseFile As String = Application.StartupPath & "\data\Cantact.accdb"

'create a backup by using Filecopy Command to copy the file from location to destination
FileCopy(databaseFile, backupfiledestination)
MessageBox.Show("پشتیبان گیری با موفقیت انجام شد.", "پیغام پشتیبان گیری", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
'catch an error
MsgBox(ex.Message)

End Try

بهتره عادت کنید که موقع ادغام دو تکه مسیر فایل یا پوشه از IO.Path.Combine استفاده کنید.
مثلا بجای "Application.StartupPath & "\data\Cantact.accdb از IO.Path.Combine(Application.StartupPath, "data\Cantact.accdb") استفاده کنید.
در اغلب موارد اتفاق بدی نمی افته ولی در بعضی حالت های خاص که ممکنه بین تکه ها تعداد "" کم و زیاد بشه استفاده نکردن از Path.Combine مشکل ایجاد می کنه.

Using dialog As New SaveFileDialog
'set the destination and a file name with the date and time
dialog.FileName = "backup_golden" & Format(Now(), "_yyyy-M-d_H-m-s") & ".accdb"
If dialog.ShowDialog() = DialogResult.OK Then
Try
Dim s = IO.Path.Combine(Application.StartupPath, "data\Cantact.accdb")
Dim backupfiledestination As String = dialog.FileName
'location of the database file that you want to backup
Dim databaseFile As String = Application.StartupPath & "\data\Cantact.accdb"
'create a backup by using Filecopy Command to copy the file from location to destination
FileCopy(databaseFile, backupfiledestination)
MessageBox.Show("پشتیبان گیری با موفقیت انجام شد.", "پیغام پشتیبان گیری", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
'catch an error
MsgBox(ex.Message)
End Try
End If
End Using

the king
چهارشنبه 08 بهمن 1399, 13:17 عصر
با سلام دوستان
من در ریشه روت یه فایل دیتابیس اکسس دارم کلیک بک آپ براش گذاشتم یعنی میخوام از اون فایل اکسس دیتابیس که در مسیر date/Cantact.accdb قرار دارد اتوماتیک کپی بگیرد و با استفاده از پنجره savedialogfile هر کجا خواستم ذخیره کنه.
ممنون میشم کمک کنید


اینم کد که در ریشه اتوماتیک ذخیره میشه میخوام با استفاده از savedialofile بتونم هر جا بخوام ذخیره کنم.

Try
'set the destination and a file name with the date and time
Dim backupfiledestination As String = Application.StartupPath & "" & "backup_golden" & Format(Now(), "_yyyy-M-d_H-m-s") & ".accdb"
'location of the database file that you want to backup
Dim databaseFile As String = Application.StartupPath & "\data\Cantact.accdb"

'create a backup by using Filecopy Command to copy the file from location to destination
FileCopy(databaseFile, backupfiledestination)
MessageBox.Show("پشتیبان گیری با موفقیت انجام شد.", "پیغام پشتیبان گیری", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
'catch an error
MsgBox(ex.Message)

End Try

بهتره عادت کنید که موقع ادغام دو تکه مسیر فایل یا پوشه از IO.Path.Combine استفاده کنید.
مثلا بجای "Application.StartupPath & "\data\Cantact.accdb از IO.Path.Combine(Application.StartupPath, "data\Cantact.accdb") استفاده کنید.
در اغلب موارد اتفاق بدی نمی افته ولی در بعضی حالت های خاص که ممکنه بین تکه ها تعداد backslash کم و زیاد بشه استفاده نکردن از Path.Combine مشکل ایجاد می کنه.

Using dialog As New SaveFileDialog
'set the destination and a file name with the date and time
dialog.FileName = "backup_golden" & Format(Now(), "_yyyy-M-d_H-m-s") & ".accdb"
If dialog.ShowDialog() = DialogResult.OK Then
Try
Dim s = IO.Path.Combine(Application.StartupPath, "data\Cantact.accdb")
Dim backupfiledestination As String = dialog.FileName
'location of the database file that you want to backup
Dim databaseFile As String = Application.StartupPath & "\data\Cantact.accdb"
'create a backup by using Filecopy Command to copy the file from location to destination
FileCopy(databaseFile, backupfiledestination)
MessageBox.Show("پشتیبان گیری با موفقیت انجام شد.", "پیغام پشتیبان گیری", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
'catch an error
MsgBox(ex.Message)
End Try
End If
End Using

karem2074
چهارشنبه 08 بهمن 1399, 20:08 عصر
داداش خیلی خیلی ممنون لطف کردید

بهتره عادت کنید که موقع ادغام دو تکه مسیر فایل یا پوشه از IO.Path.Combine استفاده کنید.
مثلا بجای "Application.StartupPath & "\data\Cantact.accdb از IO.Path.Combine(Application.StartupPath, "data\Cantact.accdb") استفاده کنید.
در اغلب موارد اتفاق بدی نمی افته ولی در بعضی حالت های خاص که ممکنه بین تکه ها تعداد backslash کم و زیاد بشه استفاده نکردن از Path.Combine مشکل ایجاد می کنه.

Using dialog As New SaveFileDialog
'set the destination and a file name with the date and time
dialog.FileName = "backup_golden" & Format(Now(), "_yyyy-M-d_H-m-s") & ".accdb"
If dialog.ShowDialog() = DialogResult.OK Then
Try
Dim s = IO.Path.Combine(Application.StartupPath, "data\Cantact.accdb")
Dim backupfiledestination As String = dialog.FileName
'location of the database file that you want to backup
Dim databaseFile As String = Application.StartupPath & "\data\Cantact.accdb"
'create a backup by using Filecopy Command to copy the file from location to destination
FileCopy(databaseFile, backupfiledestination)
MessageBox.Show("پشتیبان گیری با موفقیت انجام شد.", "پیغام پشتیبان گیری", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
'catch an error
MsgBox(ex.Message)
End Try
End If
End Using