我如何读取的电流通路| DataDirectory目录|从配置的设置 [英] How do I read the current path of |DataDirectory| from config settings

查看:374
本文介绍了我如何读取的电流通路| DataDirectory目录|从配置的设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写,要求用户选择在应用程序启动活动数据库的程序。我有一个Windows窗体,将列出存储的子文件夹中的数据库中的的ApplicationData 专门用于存储数据库文件。当我创建一个新的数据库,但是,我需要复制的模板数据库,但不能揣摩出它的默认保存。

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 of ApplicationData specifically for storing the database files. When I create a new database, however, I need to copy the the template database, but can't figure out where it's stored by default.

我试过:

dpath = ConfigurationManager.AppSettings["DataDirectory"];

我似乎总是得到的回报空值虽然。有一次,我放弃了,想我可以只设置在 DataDirectory目录以我选择的文件夹,但似乎我在我的方案,使之生效执行来不及做这个。

I always seem to get a null value in return though. At one point I gave up and figured I could just set the DataDirectory to a folder of my choice, but it seems I'm doing this too late in the execution of my program for it to take effect.

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

我想AP preciate上如​​何可以找到数据库的位置,或将其设置自己早足以影响程序操作的任何建议。

I would appreciate any suggestions on how to either find the location of the database, or set it myself early enough to affect the program operation.

编辑:

有关第二部分中,我发现,我是想以后修改连接字符串的 TableAdapter.Fill 命令已被执行,从而解释了为什么它被打开默认的数据库。这种神秘已经解决。的第一部分,但是仍然是一个未知的。

For the second part, I've discovered that I was trying to modify the connection string after a TableAdapter.Fill command had already been performed, thus explaining why it was opening the default database. That mystery has been solved. The first part, however is still a an unknown.

感谢你。

推荐答案

| DataDirectory目录| 不是来自配置设置;你混了三个不同的东西:

|DataDirectory| does not come from config settings; you're mixing up three different things:

ConfigurationManager.AppSettings["DataDirectory"]

这源于配置设置; .config文件,你必须建立并投入到项目中。这种特殊的设置是在的AppSettings 元素键DataDirectory目录元素的值。这不存在的,除非你把一个在config文件。通常,这就是你把从不更改配置或启动数据。你不应该把文件路径在这里,因为他们可以在用户机器上安装不同的数据库。

This comes from config settings; a .config file you have to create and put into your project. This particular setting is the value of the element with key "DataDirectory" in the AppSettings element. This doesn't exist unless you put one in the .config file. Typically this is where you put configuration or startup data that is never changed. You should not put file paths here, as they can be different on the machine users install your database to.

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

这是由操作系统您的应用程序被安装上定义的路径当前用户的漫游应用程序数据文件夹。你不能改变这一点,它是由操作系统定义的。你可以肯定这个文件夹是由用户可写的,如果用户漫游,或从另一台计算机登录将可用。这通常是要放在可编辑的用户数据。

This is the path to the current user's roaming application data folder defined by the operating system your app was installed on. You cannot change this, it is defined by the OS. You can be sure this folder is writable by the user, and will be available if the user roams, or logs on from another machine. This is typically where you want to put editable user data.

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

这是一个ADO.NET连接的连接字符串。 ADO.NET把竖条特别,它寻找一个AppDomain相匹配的数据竖线之间的键名。你可以得到相同的数据:

This is a connection string for an ADO.NET connection. ADO.NET treats vertical bars specially, it looks for an AppDomain data matching the key name between the vertical bars. You can get the same data with:

AppDomain.CurrentDomain.GetData("DataDirectory")

那么 DataDirectory目录的值写入?它是由什么做了部署你的可执行文件:

So what writes the value of DataDirectory? It's done by whatever deploys your executable:


  • .MSI安装程序将其定义为应用程序的目标文件夹。

  • 的ClickOnce定义了一个特殊的数据文件夹在你的项目。

  • Web应用程序使用App_Data文件夹中。

  • Visual Studio调试器使用debug文件夹中。

请注意该.MSI安装可以允许用户改变DataDirectory目录;这就是为什么你不应该硬code或者换 DataDirectory目录,如果你这样做,有没有办法找到您的应用程序数据部署。您通常使用 DataDirectory目录文件夹部署的可执行文件的只读二进制数据。

Note that .MSI installers can allow the user to change the DataDirectory; this is why you should never hard-code or change DataDirectory, if you do that there is no way to find where your application data was deployed. You typically use the DataDirectory folder for read-only binary data deployed with your executable.

如果你需要写部署你的可执行文件,你应该首先将其复制某个地方你知道用户将能够写入,如 Environment.SpecialFolder.ApplicationData ,并写入副本。不仅是 DataDirectory目录不一定由用户可写的,它是在部署的一部分,而不是用户数据的一部分;如果您修复或卸载你的可执行文件,然后 DataDirectory目录被重新安装或删除。当你删除他们的数据,所以不要把它保存到 DataDirectory目录用户不喜欢它。

If you need to write to the data deployed with your executable you should first copy it someplace you know the user will be able to write to, such as to Environment.SpecialFolder.ApplicationData, and write to the copy. Not only is DataDirectory not necessarily writable by users, it is part of the deployment and not part of the user data; if you repair or uninstall your executable then DataDirectory gets reinstalled or deleted. Users don't like it when you delete their data, so don't save it to DataDirectory.

这篇关于我如何读取的电流通路| DataDirectory目录|从配置的设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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