How do I read the current path of |DataDirectory| from config settings

后端 未结 3 2159
花落未央
花落未央 2020-12-01 20:12

I\'m writing a program that requires the user to select the active database at application startup. I have a Windows Form that will list the databases stored in a sub-folder

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-01 20:31

    The Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) code will return a null value in development every time. The |DataDirectory| is set when the program is installed.

    Build the project, then install and test it.

    I used that line of code to compact a database in an installed application at runtime.

    That code can be set to a variable, as follows... Dim beer as strong

    Beer = Environmenr.GetFolderPath(Environment.SpecialFolder.ApplicationData)
    

    This will return the folder path for the INSTALLED |DataDirectory|. Add the database name on with a CStr and another variable...

    Dim MyPathA As String = CStr(Beer & "\Workout.mdb")
    Dim MyPathB As String = CStr(Beer & "\BackupWorkout.mdb")
    
    Dim JRO As JRO.JetEngine
    JRO.CompactDatabase(CStr("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & MyPathA), _
    CStr("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & MyPathB & ":Jet OLEDB:Engine Type=5"))
    
    On error Errorhandler, 
    Errothandler
    Kill(MyPathB)
    

    The 1st line is your database, the 2nd line renames it to Backup and compacts it in the same directory. If there is a Backup there, it will trigger the error, which will delete the backup.

    After that, say this is button click. Run it all again. Right after the kill line,

    Me.Buttonx.PerformClick()
    

    That is how to compact a database in an installed ClickOnce application. Using |DataDirectory| in the code will throw an illegal characters error...

提交回复
热议问题