|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://stackoverflow.com/questions/12266924/how-do-i-read-the-current-path-of-datadirectory-from-config-settings

最新文章

  1. 走读openwrt的shell的总结【转】
  2. [AIR] as3 之条件编译多平台妙用
  3. Android SDK在线更新镜像服务器大全
  4. The include feature of SQL Server Index
  5. asp.net 配置 X-Frame-Options
  6. [cocos2d-js]chipmunk例子(一)
  7. Resource temporarily unavailable
  8. DirectShow系统初级指南
  9. Attributes(1):反射Attribute并输出
  10. WebApi(四)-Post接口请求失败或接受不到参数(解决方法)
  11. 调试exynos4412—ARM嵌入式Linux—LEDS/GPIO驱动之二
  12. iOS点滴- ViewController详解
  13. NOIP2017SummerTraining0705
  14. bzoj 4872: [Shoi2017]分手是祝愿 [期望DP]
  15. spark调用hdfsAPI
  16. tensorflow 1.0 学习:用CNN进行图像分类
  17. HDU1711-KMP-水题
  18. Spark学习笔记——读写Hbase
  19. ubuntu数据库安装配置
  20. linux page allocation and deallocation

热门文章

  1. C#在winform中调用系统控制台输出
  2. C#基础-out与ref字段
  3. Web 分页
  4. ADO.net数据绑定
  5. C#File类常用的文件操作方法(创建、移动、删除、复制等)
  6. 说说&和&&的区别
  7. navicat怎么导出和导入数据表
  8. JAVA Collections工具类sort()排序方法
  9. ASP.NET Core 运行原理剖析1:初始化WebApp模版并运行
  10. 一个强大的jquery分页插件