问题
I am working on a VB.NET / WPF
application which will use SQL Compact Edition
databases.
The application should allow the user to save and load different versions of the database.
To do this, my intention was to have a standard database name (e.g. myDatabase.sdf) which would be saved in the DataDirectory
.
Then the user would have an option to save a version of the current data (calling it what they want e.g. savedDatabase1.sdf) and the application would then take a copy of the database from DataDirectory
and save it to another location (e.g. a SavedDatabase folder created in the Windows app data area
)
and to load a different version of the database, the application could copy the database from the SavedDatabase folder and overwrite the database in the DataDirectory
location.
I can see solutions for overriding the data directory location, but I can't find any code which allows you to retrieve the path of the current data directory folder so it can be used in any file copy activities as described above.
So my question is - How do I programmatically retrieve the full path currently being used as the data directory?
回答1:
You could save all the data in the same folder as the application:
System.AppDomain.CurrentDomain.BaseDirectory
However if you are not sure you have access to that folder you can always write to the Application Data which is made for this:
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
Then in this location you can create your own application directory and then your sub directories.
simple example would be:
Dim DataPath as String = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "/MyApplication/Data/"
connectionString="Data source=|" & DataPath &"myDatabase.sdf;"
来源:https://stackoverflow.com/questions/43737622/programatically-retrieve-the-full-data-directory-path-in-vb-net