winform的配置文件|C# WinForm程序怎么写配置文件

|

Ⅰ C# WinForm程序怎么写配置文件

//配置文件的源文件(App.Config) <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="ServerName" value=""/> </appSettings> </configuration> //更新app.config的函数private void UpdateConfig(string Xvalue){ XmlDocument doc = new XmlDocument(); doc.Load(Application.ExecutablePath+".config"); XmlNode node = doc.SelectSingleNode(@"//add[@key='ServerName']"); XmlElement ele = (XmlElement)node; ele.SetAttribute("value",Xvalue); doc.Save(Application.ExecutablePath+".config"); } web.config: /// <summary> /// 向.config文件的appKey结写入信息AppValue 保存设置 /// </summary> /// <param name="AppKey">节点名</param> /// <param name="AppValue">值</param> Private void SetValue(String AppKey,String AppValue){ Xmldocument xDoc=new XmlDocument();xDoc.Load(System.Windows.Forms.Application.ExecutablePath+”.config”);XmlNode xNode;XmlElement xElem1;XmlElement xElem2;xNode=xDoc.SelectSingleNode(“//appSettings”);xElem1=(XmlElement)xNode.SelectSingleNode(“//add[@key=’”+AppKey+”’]”);if(xElem1!=null)xElem1.SetAttribute(“value”,AppValue);else{ xElem2=xdoc.CreateElement(“add”); xElem2.SetAttribute(“key”,AppKey); xElem2.setAttribute(“value”,AppValue); xNode.AppendChild(xElem2);}xDoc.Save(System.Windows.Forms.Application.ExecutablePath+”.config”);}本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/pengfeili/archive/2008/04/19/2306407.aspx

Ⅱ C#中配置文件是什么意思,有什么用,保存在什么地方

配置文件是用xml写的一个文件,因为xml更改后不用重新生成就可以使用,所以用来配置一些可能经常改动的东西。举个例子,数据库连接字符串就经常通过配置文件来配置:这样的好处,你写代码的时候,用的数据库是你自己的,你的程序打包后,在另外的一台电脑上安装使用,如果不是连配置文件,那么那台电脑必须能连接上你的电脑,否则就不能用了。通过配置文件,他可以直接在配置文件中把数据库改成他自己的。那样就好用,并且不用任何的编译。项目文件一般分两种:app.config,(用于非web程序),相应的,web.config用于web项目向项目添加配置文件的时候,一般不建议更改名字,使用默认的就好。 public string filepath = ConfigurationManager.AppSettings["path"]的意思是从app.config文件中配置的一个key,这行代码字面上看是读取配置文件中存放file的路径ConfigurationManager是.netframework中提东操作配置文件的类库,Appsettings[]里面的字符串是在app.config中定义的类似这样的<add key="path" value="c:\\system32"/>

Ⅲ Winform 程序中的配置文件app.config生成时能不能改名

如果你的exe名字叫A.exe,那么生成时会自动改成A.exe.config,不能改成其他名字。

Ⅳ Winform中,配置文件怎么写来与数据库连接

添加一个应用程序配置文件,App.Config在<configuration>节点下添加 <connectionStrings>节点, <add name = "" connectionstring = "">剩下的和内Web.Config中差不多了容。

Ⅳ c# winform 程序打包中,在读取配置文件时 ,有错误,求解

//配置文件的源文件(App.Config) <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="ServerName" value=""/> </appSettings> </configuration> //更新app.config的函数private void UpdateConfig(string Xvalue){ XmlDocument doc = new XmlDocument(); doc.Load(Application.ExecutablePath+".config"); XmlNode node = doc.SelectSingleNode(@"//add[@key='ServerName']"); XmlElement ele = (XmlElement)node; ele.SetAttribute("value",Xvalue); doc.Save(Application.ExecutablePath+".config"); } web.config: /// <summary> /// 向.config文件的appKey结写入信息AppValue 保存设置 /// </summary> /// <param name="AppKey">节点名</param> /// <param name="AppValue">值</param> Private void SetValue(String AppKey,String AppValue){ Xmldocument xDoc=new XmlDocument();xDoc.Load(System.Windows.Forms.Application.ExecutablePath+”.config”);XmlNode xNode;XmlElement xElem1;XmlElement xElem2;xNode=xDoc.SelectSingleNode(“//appSettings”);xElem1=(XmlElement)xNode.SelectSingleNode(“//add[@key=’”+AppKey+”’]”);if(xElem1!=null)xElem1.SetAttribute(“value”,AppValue);else{ xElem2=xdoc.CreateElement(“add”); xElem2.SetAttribute(“key”,AppKey); xElem2.setAttribute(“value”,AppValue); xNode.AppendChild(xElem2);}xDoc.Save(System.Windows.Forms.Application.ExecutablePath+”.config”);}本文来自CSDN博客,转载请标明出处:

Ⅵ C# 怎么保存WinForm应用程序的配置

供参考:1.自定义一个配置文件(.config/.txt),比如:在BIN目录下生成一个setting.config,通过winform界面把配置参数全部保存到这里面来。2.保存到app.config中,可以把一些配置参数保存到app.config,这样在窗体编译的时候,app.config会自动生成到BIN目录下。3.保存到数据库指定的表,比如:D_DataBase/T_Setting表,可以通过winform界面把参数全部保存到数据库指定的表中。4.保存到注册表中。

Ⅶ winform 如何动态的读写配置文件

ini config xml等都可以作为配置文件。你说的是自带的web.config文件吧?它的读取很复杂,有些标签专很好读,有的就复杂,因为配置属节太多,更重要的是,web.config是微软提供的,需要高度安全的配置文件,所以在一些配置节的操作上,不提供写,xml文件的写入都是通过先删除在添加的形式,所以这是不提供删除没就没办法完成写入。最好用其他文件做配置文件。

Ⅷ C# winform 的配置文件用什么保存好,txt,ini.还是xml.

都可以,看你自己选择了txt需要自定义格式xml可以用XmlDocument操作ini要通过DllImport("kernel32")来引入外部函数操作

Ⅸ winform 运行中修改了配置信息,怎么重新读取配置文件

这是因为你程序运行时,已经将配置文件中的信息加载到了内存中,之后每次读取时如果缓存中已经存在对应的值,则直接使用此值,否则才会从文件中读配置,这样做的好处是减少了系统和文件甚至与数据库的交互次数;在web程序中配置文件更改后,应用程序会自动重启一次,于是配置会自动生效。但winform程序没有这个机制,于是Configuration.ConfigurationManager调用配置不会自动更新。所以建议你手动实现调用配置的逻辑,代码如下:public string ReadAppSetting(string key) { string xPath = "/configuration/appSettings//add[@key='"+key+"']"; XmlDocument doc = new XmlDocument(); string exeFileName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name; doc.Load(exeFileName + ".exe.config"); XmlNode node = doc.SelectSingleNode(xPath); return node.Attributes["value"].Value.ToString(); }这样做的话就不存在缓存的问题了,希望能对你有所帮助。

Ⅹ c#winform怎么写本地配置文件

配置文件可以是任何形式,可以是xml或者txt都行,比如数据库的连接配置比如:<?xml version="1.0" standalone="yes"?>//这句一定要有,下面的你随意写<configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" /> </startup> <connectionStrings> <add name="DBConnectionString" connectionString="Data Source=192.168.118.21;Initial Catalog=PreMix;User Id =sa;Password =sa" providerName="System.Data.SqlClient" /> </connectionStrings></configuration>这就是个配置文件,在winform里面直接用DataSet的ReadXml()方法就能读取到里面的值,简单例子:<?xml version="1.0" standalone="yes"?>//这句一定要有,下面的你随意写<a> <b>你好啊!</b></a>然后你在winform里DataSet的ReadXml(XML文件路径),然后得到的就是a表里的b列的数据,就是你好啊!,就得到了,看看表的视图就清楚了。不懂继续追问


赞 (0)