C#中的配置文件 [英] Configuration file in C#

查看:119
本文介绍了C#中的配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们好,



我在Window应用程序中工作,但我需要的是应用程序从文本文件读取数据库连接。这是有效的,但我希望当我发布我的应用程序时,它也发布在同一文件夹中的txt文件。这就是为什么当需要通过txt文件更改连接字符串时。



当安装此exe时,txt文件也将保存在本地用户系统中。

Hello friends,

I am working in a Window Application but my need is application read database connection from text file . That is working but I want when I publish my Application its also Publish txt file in same folder. Thats why when ever need to change in connection string its by txt file.

ANd When Install this exe then also txt file will be save in local user system.

推荐答案

首先,确保有一个到MySQL数据库的正确连接字符串( http: //connectionstrings.com/mysql [ ^ ])。



忘记文本文件。更确切地说,忘记了对文本文件的解释,因为.NET提供了开箱即用的解决方案。它被称为app.config,并且它有一个仅用于连接字符串的部分。点击此处:如何从App获取连接字符串.Config in c# [ ^ ]。迁移时发现这些也很有用:在应用程序中配置连接字符串。在C#中运行期间配置文件 [ ^ ], http://msdn.microsoft。 com / en-us / library / ms171887.aspx [ ^ ]



请注意,app.config(XML)是自动构建的,如果将其包含在安装工具箱中,它也被部署。



您甚至可以保护连接字符串。点击此处: http://msdn.microsoft.com/en-us/ library / 89211k9b(v = vs.80).aspx [ ^ ]
First of all, be sure to have a proper connection string to your MySQL database (http://connectionstrings.com/mysql[^]).

Forget the text file. More precisely forget your interpretation of a text file, since .NET has the solution out of the box. It is called app.config, and that one has a section just for connection strings. Check here: How to get Connection String from App.Config in c#[^]. You migth find these also useful: Configuring a Connection String in the App.Config File During Runtime in C#[^], http://msdn.microsoft.com/en-us/library/ms171887.aspx[^]

Please note, that the app.config (an XML) is automatically built, and if you include it in your install kit, it is also deployed.

You can even secure the connection strings. Check here: http://msdn.microsoft.com/en-us/library/89211k9b(v=vs.80).aspx[^]


您好,



试试这个



在那个文本文件中只是把在线数据库名称不是路径



Ex:



Provider = Microsoft.Jet.OLEDB.4.0;数据源=#FilePath#\ DB_New.mdb





代码



从txt文件中读取Connstring并替换



Hi,

Try This

In that Text file Just put Online DB Name Not Path

Ex:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source= #FilePath# \DB_New.mdb


in code

Read Connstring from txt File and replace

string constring = "";//Read From txt File
string path = Application.ExecutablePath;
string resultPath = path.Substring(0, path.LastIndexOf("\\"));

constring.replace("#FilePath#",resultpath);





注意:如果您的数据库位置位于发布EXE文件的同一文件夹中,它将进行锻炼



谢谢



Siva Rm K



Note : It will workout if Your DB Location reside in the same folder of publish EXE File

Thanks

Siva Rm K


这篇关于C#中的配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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