0

I have code which asks the user for a folder path and then copies an MDB file to that folder for back-up. However, the MDB is being copied from the wrong folder. How can I fix it so that it uses the right data folder when backing-up and restoring the MDB?

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    Try
        Dim fbd As New FolderBrowserDialog
        If fbd.ShowDialog() = vbOK Then
            System.IO.File.Copy("MHC.mdb", fbd.SelectedPath & "\MHC.mdb")
            MsgBox("Done")
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Private Sub btnRestore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRestore.Click
    Try
        Dim fbd As New FolderBrowserDialog
        If fbd.ShowDialog() = vbOK Then
            File.Delete("MHC.mdb")
            System.IO.File.Copy(fbd.SelectedPath & "\MHC.mdb", "MHC.mdb")
            MsgBox("Done")
            Application.Restart()
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
user7806296
  • 1
  • 2
  • 3
  • 6
  • It was very difficult to understand what you were asking, due to your choppy English. I did my best to interpret what you were trying to say and rewrite it in a more readable way. Let me know if I lost your meaning in translation. – Steven Doggart Dec 01 '17 at 00:52
  • What folder path is being selected and what is the path to which the file is actually being copied? In other words, what are you expecting to happen and what is the actual result? – Steven Doggart Dec 01 '17 at 01:01
  • All we can tell you from that code is that the file will be copied into the folder whose path is contained in `fbd.SelectedPath`. You need to be far more specific. Tell us what folder path was selected and the path of the folder the file was copied to. Show us screenshots to prove that what you claim to be the case is the case because there's nothing in that code to account for it. – jmcilhinney Dec 01 '17 at 01:02
  • 1
    By the way, you should start by turning `Option Strict On` and then comparing the result of `ShowDialog` to actual an `DialogResult` value, i.e. `DialogResult.OK`. You should also use `Path.Combine` to combine partial file and folder paths. Finally, you should not use file names alone but always use full file paths. If you want the file from the folder the EXE was run from then use `Path.Combine(Application.StartupPath, "MHC.mdb")`. – jmcilhinney Dec 01 '17 at 01:04
  • hi sir thank you for editing my question. – user7806296 Dec 01 '17 at 01:12
  • sir this is the location that i want to locate C:\Users\Desktop\Morse v2.6\Morse\bin\Debug\Data\MHC.mdb but the file is saving and restoring to other path like, – user7806296 Dec 01 '17 at 01:13
  • C:\Users\Desktop\Morse v2.6\Morse and C:\Users\Desktop\Morse v2.6\Morse\bin\Debug – user7806296 Dec 01 '17 at 01:14
  • do you know sir why is this happening? using my codes? – user7806296 Dec 01 '17 at 01:15
  • I don't understand. Is it going to the folder that is being selected by the user? Or is it going somewhere different than the folder selected? – Steven Doggart Dec 01 '17 at 01:16
  • Is it going to the folder that is being selected by the user sir. – user7806296 Dec 01 '17 at 01:19
  • If it's going to where the user selects, then what is the problem? Do you need the folder browser dialog to default to a different starting folder? Or do you need the dialog to limit the user's choices? – Steven Doggart Dec 01 '17 at 01:21
  • No sir i just want to know the proper declaration of location of that file MHC.mdb to save backup on it and restore it again in the same location if there is some changes or update in that in the system. – user7806296 Dec 01 '17 at 01:34
  • So it backs up to the correct folder, it just doesn't restore it to the right place? – Steven Doggart Dec 01 '17 at 01:35
  • Both back up and restore is not getting the proper file location, that's why the saved file is not correct and the new restored data is not updating the exist data in the system. – user7806296 Dec 01 '17 at 01:41
  • I think that we may have lost some info when @StevenDoggart cleaned up the posting and the `FolderBrowserDialog` is a red herring. ==> `"Because i have some issue that the save and restore is not saving and restoring to the Data folder but in other folder of the system."` When you state "Data Folder", do you mean the `|DataDirectory|` used in the connection string? Do you need to know how to obtain the `|DataDirectory|` path? – TnTinMn Dec 01 '17 at 02:41
  • Yes I want to know the DataDirectory path. because when i save the backup file MHC.mdb the system copied the wrong updated file and when i restore it, it goes to other folder file under the system, that's why the system did not update the database. – user7806296 Dec 01 '17 at 07:32
  • In that case, what's the connection string you're using to open the MDB? – Steven Doggart Dec 01 '17 at 11:37
  • thank you helping me now i get it already – user7806296 Dec 05 '17 at 06:37
  • but now i have something to add in the filename so that i can save it without overlapping to the existing one. – user7806296 Dec 05 '17 at 06:39
  • do you have some idea how can i do that? – user7806296 Dec 05 '17 at 06:39

1 Answers1

1

Yes I want to know the DataDirectory path.

The |DataDirectory| path placeholder as used in a connection string similar to this example:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Northwind.MDB

may be retrieved using code similar to this:

Dim objDataDir As Object = AppDomain.CurrentDomain.GetData("DataDirectory")
Dim dataDirectory As String = TryCast(objDataDir, String)

Beware that the above code can return Nothing (null) for dataDirectory. The only scenario that I know of that sets a "DataDirectory" is an application published using ClickOnce.

If dataDirectory is null, then you probably want to assign the Application.StartupPath property to it.

I do not know of any official documentation that states this is the proper procedure, but the code is based off of the ExpandDataDirectory method.

There is also a non-publicly scoped DataDirectory Property on the AppDomain.CurrentDomain.ActivationContext object, but you would need to use reflection to retrieve that property.

Note that if you want to use the |DataDirectory| placeholder, but point it to a path of your choice, you can use:

AppDomain.CurrentDomain.SetData("DataDirectory", "your path here")
TnTinMn
  • 11,522
  • 3
  • 18
  • 39
  • Thanks for the help. I'm still not positive this will be what the OP needs, but it should hopefully help. – Steven Doggart Dec 01 '17 at 18:14
  • 1
    @StevenDoggart, Thanks. I originally thought that this info had to be in an existing question, but after a quick review of questions like [To Get DataDirectory Path in VB.net?](https://stackoverflow.com/questions/18568897/to-get-datadirectory-path-in-vb-net), I realized this info is hidden better than I thought. So if of no use to this poster, maybe it will be helpful to someone else. – TnTinMn Dec 01 '17 at 18:21
  • I've never even heard of it before, so yeah, it's definitely not widely advertised. Here's a [related c# question](https://stackoverflow.com/questions/1409358/ado-net-datadirectory-where-is-this-documented) which is helpful too. – Steven Doggart Dec 01 '17 at 18:27