以编程方式检索VB.NET中的完整数据目录路径 [英] Programatically retrieve the full data directory path in VB.NET

查看:168
本文介绍了以编程方式检索VB.NET中的完整数据目录路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SQL Compact Edition数据库的VB.NET / WPF应用程序上工作.

I am working on a VB.NET / WPF application which will use SQL Compact Edition databases.

应用程序应允许用户保存和加载不同版本的数据库.

为此,我的目的是要拥有一个标准的数据库名称(例如myDatabase.sdf),该名称将保存在DataDirectory中.

To do this, my intention was to have a standard database name (e.g. myDatabase.sdf) which would be saved in the DataDirectory.

然后,用户可以选择保存当前数据的版本(调用所需的名称,例如,saveDatabase1.sdf),然后应用程序将从DataDirectory中获取数据库的副本并将其保存到另一个位置(例如SavedDatabase folder created in the Windows app data area) 并加载其他版本的数据库,应用程序可以从SavedDatabase文件夹复制数据库并覆盖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.

所以我的问题是-如何以编程方式检索当前用作数据目录的完整路径?

推荐答案

您可以将所有数据与应用程序保存在同一文件夹中:

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.

一个简单的例子是:

Dim DataPath as String = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "/MyApplication/Data/"
connectionString="Data source=|" & DataPath &"myDatabase.sdf;"

这篇关于以编程方式检索VB.NET中的完整数据目录路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆