How to read the current path | DataDirectory | from configuration settings

I am writing a program that requires the user to select the active database when the application starts. I have a Windows form that will list the databases stored in the ApplicationData strong> subfolder , specifically for storing database files. However, when I create a new database, I need to copy the template database, but I can not determine where it is stored by default.

I tried:

dpath = ConfigurationManager.AppSettings["DataDirectory"]; 

In any case, I always get a null value. At some point, I gave up and decided that I could just install the DataDirectory into a folder of my choice, but it seems like I'm executing my program too late for it to take effect.

 newdpath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\MyAppFolder"; 

I would appreciate any suggestions on how to find the location of the database or install it early enough to affect the program.

EDIT:

In the second part, I found that I was trying to change the connection string after the TableAdapter.Fill command was already executed, explaining why it opened the database by default. This mystery has been resolved. The first part, however, remains unknown.

Thanks.

+5
source share
2 answers

|DataDirectory| not obtained from configuration settings; you mix three different things:

 ConfigurationManager.AppSettings["DataDirectory"] 

This comes from the configuration settings; a .config file that you must create and place in your project. This particular setting is the value of the item with the "DataDirectory" key in the AppSettings item. This does not exist unless you put it in a .config file. Typically, here you put configuration or startup data that never changes. Here you should not indicate the paths to files, as they may be different on the computer where users install your database.

 Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) 

This is the path to the current application data folder for the user, determined by the operating system on which the application is installed. You cannot change this; it is determined by the OS. You can be sure that this folder is writable by the user and will be available if the user switches to another computer or logs in. This is usually the place where you want to put editable user data.

 SqlConnection("Data Source=|DataDirectory|DatabaseFileName.sdf;...") 

This is the connection string for the ADO.NET connection. ADO.NET handles vertical columns on purpose; it looks for AppDomain data matching the key name between the vertical bars. You can get the same data with:

 AppDomain.CurrentDomain.GetData("DataDirectory") 

So what does the value of DataDirectory ? This is done regardless of the deployment of the executable:

  • . MSI installers define it as the application’s destination folder.
  • ClickOnce defines a special data folder in your project.
  • Web applications use the App_Data folder.
  • The Visual Studio debugger uses the debug folder.

Note that .MSI installers may allow the user to modify the DataDirectory; therefore, you should never hardcode or modify the DataDirectory , if you do this, to find where the application data has been deployed, there is no way. Usually you use the DataDirectory folder for read-only binary data deployed with your executable.

If you need to write data deployed along with your executable file, you must first copy it where you know that the user can write, for example, Environment.SpecialFolder.ApplicationData , and write to a copy. Not only is DataDirectory not necessarily written by users, it is part of the deployment, not part of user data; if you restore or delete the executable, DataDirectory will be reinstalled or deleted. Users do not like it when you delete their data, so do not save it to a DataDirectory .

+23
source

The code Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) will return a null value each time during development. | DataDirectory | installed during program installation.

Build the project, then install and test it.

I used this line of code to compress the database in the installed application at runtime.

This 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 INSTALLED | DataDirectory |. Add the database name using 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) 

1st row is your database, 2nd row renames it to Backup and compresses it to the same directory. If there is a backup there, it will cause an error that will delete the backup.

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

 Me.Buttonx.PerformClick() 

This is how you can compress the database in the installed ClickOnce application. Use | DataDirectory | the code will throw an invalid character error ...

-1
source

Source: https://habr.com/ru/post/1432312/


All Articles