Backing up and restoring a file

Posted on 2004-10-13
Last Modified: 2010-04-24
I am trying find some code to do a simple backup and resotore of a Data.ini file, In short; I have a Data.txt file in a directory called Support Files that I access with Application.StartupPath() I need a button to make a copy of the Data.txt file in the Support Files directory and place the copy of the file in a directory Support Files\Backups but have it renamed to a format of the name "Data" plus the date it was saved on and the .txt extension.  

If for some reason the user messes up the main Data.txt file in the Support Files directory I also need a button to allow them to open the Support Files\Backups directory and select the file they want to replace the bad Data.txt file with.  It will then overwrite the Data.txt file in the Support Files directory with the file from the Support Files\Backups directory and leave the backup copy in the Support Files\Backups directory too.
Question by:teamdad
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 86

Accepted Solution

Mike Tomlinson earned 500 total points
ID: 12302541
Add an OpenFileDialog control from the toolbox to your form and use the following code:

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim supportPath As String = Application.StartupPath() & "\Support Files\"
        Dim backupPath As String = supportPath & "\Backups\"

        OpenFileDialog1.Title = "Please select a Backup INI file to Restore"
        OpenFileDialog1.InitialDirectory = backupPath
        OpenFileDialog1.Filter = "Backup INI files (*.ini)|*.ini"

        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            Dim sourceFile As String = OpenFileDialog1.FileName
            Dim doubleCheck As String = _
                "Your original Data.ini will be replaced with:" & vbCrLf & _
                sourceFile & vbCrLf & vbCrLf & _
                "Are you sure you want to continue?"
            If MessageBox.Show(doubleCheck, "Restore Backup INI", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) = DialogResult.Yes Then
                Dim targetFile As String = supportPath & "Data.ini"
                If System.IO.File.Exists(targetFile) Then
                End If
                System.IO.File.Copy(sourceFile, targetFile)
            End If
        End If
    End Sub
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12302579

In your first sentence you stated that the file was Data.ini, but in all subsequent references it is Data.txt.

If the file is indeed data.txt and not data.ini, then change all occurrences of "ini" in the code with "txt".

LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12302638
You should still be able to use the code srcalc gave you to make a Backup of the file:

    Dim datapath = Application.StartupPath & "\Support Files\"
    Public Sub backupData()
        If Not File.Exists(datapath & "Data.ini") Then
            MsgBox("There is no data to copy!")
            File.Copy(datapath & "Data.ini", datapath & "Backups\Data" & Now.Month & "-" & Now.Day & "-" & Now.Year & " " & Now.Hour & "-" & Now.Minute & "-" & Now.Second & ".ini")
        End If
    End Sub


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question