Programatically retrieve the full data directory path in VB.NET

眉间皱痕 提交于 2019-12-24 21:04:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!