ADO.net 连接字符串中的 |DataDirectory| 是什么

时间:2023-01-12 17:13:59

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

ConfigurationManager.AppSettings["DataDirectory"]

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;...")

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")

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

  • .MSI installers define it as the app's target folder.
  • ClickOnce defines a special data folder in your project.
  • Web apps use the App_Data folder.
  • The Visual Studio debugger uses the debug folder.

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.

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.

 

查看:

https://*.com/questions/12266924/how-do-i-read-the-current-path-of-datadirectory-from-config-settings

ADO.net 连接字符串中的 |DataDirectory| 是什么的更多相关文章

  1. 转:ADO.NET连接字符串

    名称 ADO.NET连接字符串 说明 ADO.NET连接字符串:SQL Server,SQL Server 2005,ACCESS,Oracle,MySQL,Interbase,IBM DB2,Syb ...

  2. 连接字符串中Min Pool Size的理解是错误,超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。

    Min Pool Size的理解是错误的 假设我们在一个ASP.NET应用程序的连接字符串中将Min Pool Size设置为30: <add name="cnblogs" ...

  3. ADO&period;NET连接字符串大全---各种数据库的连接字符串

    ADO.NET连接字符串大全 ADO.NET连接字符串 名称 ADO.NET连接字符串 说明 ADO.NET连接字符串:SQL Server,SQL Server 2005,ACCESS,Oracle ...

  4. ADO&period;NET连接字符串大全

    说明ADO.NET连接字符串:SQL Server,SQL Server 2005,ACCESS,Oracle,MySQL,Interbase,IBM DB2,Sybase,Informix,Ingr ...

  5. &lbrack;穷尽&rsqb;ADO&period;NET连接字符串

    微软提供的四种数据库连接方式: System.Data.OleDb.OleDbConnection System.Data.SqlClient.SqlConnection System.Data.Od ...

  6. 【转载】两个Web&period;config中连接字符串中特殊字符解决方案

    userid =  test password = aps'"; 那么连接字符串的写法为: Provider=SQLOLEDB.1;Password="aps'"&quo ...

  7. db2 jdbc连接字符串中 指定currentSchema

    场景:连接DB2数据库的,jdbc的连接字符串中没有给当前的数据源用户指定默认的schema,而当前的数据源用户下可能有多个schema,则会使用数据源用户默认的schema. 例如:admin用户的 ...

  8. Ado&period;net连接字符串

    学习刘皓的 文章ADO.NET入门教程(三) 连接字符串,你小觑了吗? 连接字符串主要有DataSource 指定地址 通常是ip 如果Express 就要使用形如 ./Express或者 (loca ...

  9. 关于打开现有项目时数据库连接配置遇到的问题 连接字符串中的数据源值指定未安装的SQL Server的实例。要解决此问题,可选择安装匹配的SQL Server实例或修改连接字符串中的数据源值

    最近在看红皮书<ASP.NET MVC 5 高级编程>时,为了更好理解,边看书,边打开源代码查看,在VS(Visual Studio 2015)中将源代码打开,发现数据库连接是关闭的,本想 ...

随机推荐

  1. Python——Day3知识点——文件操作

    一.打开文件 文件句柄 = open('文件路径', '模式') 打开文件时,需要指定文件路径和以何等方式打开文件,打开后,即可获取该文件句柄,日后通过此文件句柄对该文件操作. 打开文件的模式有: r ...

  2. CentOS详解top命令各个数据的含义

    最近在docker(宿主机是centos虚拟机)里安装gitlab,发现随着时间的运行,虚拟机的内存持续走高,运行几个小时之后内存已经爆掉了,putty远程处理于假死状态. 这个时候就需要查看内存进程 ...

  3. flush vs ob&lowbar;flush

    刷新PHP程序的缓冲,而不论PHP执行在何种情况下(CGI ,web服务器等等).该函数将当前为止程序的所有输出发送到用户的浏览器. flush() 函数不会对服务器或客户端浏览器的缓存模式产生影响. ...

  4. 微信公众平台开发详细步骤与java代码

    1.微信公众平台设置 首先在https://mp.weixin.qq.com/注册一个公众平台账号(服务号.订阅号.企业号的区别) 微信公众平台地址:https://mp.weixin.qq.com ...

  5. js判断终端是手机还是电脑

    $(function(){ function browserRedirect() { var sUserAgent= navigator.userAgent.toLowerCase(); var bI ...

  6. 超出区域文字显示为省略号或者剪切效果——CSS的生僻操作

    每门语言总有那么一些不太常用,却又不可或缺的语法操作.下面说一下,在HTML文件中,如何设置用 "省略号" 和 "剪切" 代替大量文字的效果. 以下是HTML的 ...

  7. Directory Opus(DO) 个人使用经验 1&period;0

    设置语言为中文 即时过滤器 设置好之后,在文件目录直接先点击“ ; ”键,然后就可以即时过滤了. 自带的图片查看器查看图片时适应窗口 设置默认窗口 将当前打开的窗口配置为默认窗口,以后每次重新打开DO ...

  8. Python可视化库-Matplotlib使用总结

    在做完数据分析后,有时候需要将分析结果一目了然地展示出来,此时便离不开Python可视化工具,Matplotlib是Python中的一个2D绘图工具,是另外一个绘图工具seaborn的基础包 先总结下 ...

  9. C&plus;&plus;语言学习

    1.< >表示包含那些由系统提供的并放在指定子目录中的头文件,对于自己编写的头文件放在当前目录或其他目录下则用双引号” ”; 2.复合类型的声明: int* p1,p2;//p1是指向in ...

  10. python3&period;6&period;1 安装PyQt5,以及配置QTDesigner,PyUIC

    本人主机win10 64,python版本是3.6.1 64 注意python版本一定得是3.6.1 64位的,我原来电脑是安装的32位的,浪费了好长时间 (MMP) 第一步:安装python,自己官 ...