使用.config文件在C#中创建EXE [英] Create EXE in C# with .config file

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

问题描述

全部好,



我创建了一个基本软件,将数据添加到sql并进行编辑。



我在App.config文件的帮助下创建了sql connect。



现在我想把这个软件exe给客户端,也可以选择在用户级别更改sql路径连接字符串。



如果我正在装箱设置文件,我如何根据客户端机器更改细节。



请帮忙。



我尝试了什么:



我试图创建设置但无法添加配置文件请帮助

HI all,

I have created one basic software adding data to sql and editing the same .

I have created sql connect with the help of App.config file.

Now I want to give that software exe to client and also the option to change sql path connection string at user level .

if I am crating a setup file how can i change details according to client machine.

please help.

What I have tried:

I tried to create setup but not able to add config file please help

推荐答案

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  
var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");  
if(connectionStringsSection != null)  
{  
   connectionStringsSection.ConnectionStrings["aConnectionName"].ConnectionString = "server=Your new connection information";  
   config.Save();  
   ConfigurationManager.RefreshSection("connectionStrings"); 
} 





我假设您可以在允许更改之前验证新连接字符串。



I assume you will have a way to validate the new connection string before allowing it to be changed.


我是这样做的:实例存储 - 在应用程序之间共享配置数据的简单方法 [ ^ ]
Here is how I do it: Instance Storage - A Simple Way to Share Configuration Data among Applications[^]


您可以按如下方式找到用户配置文件的位置:

You can find the location of the user config file as follows:
var pathUserConfig = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;

如果您希望用户能够编辑您可以在TextBox中显示文件内容的设置。

If you want the user to be able to edit the settings you can show the file contents in a TextBox.


这篇关于使用.config文件在C#中创建EXE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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