日期:2014-05-18  浏览次数:20779 次

C#如何创建一个可以让应用程序读写的配置文件?
各位, 我想要做个配置文件, 可以让一个应用程序EXE,来读取某个值,

向大家请教下具体的步骤。

------解决方案--------------------
在项目中添加应用程序配置文件,在程序中使用 System.Configuration.ConfigurationManager.AppSettings();读取值!
不知道你是要这个意思吗?
------解决方案--------------------
在项目中新建一个xml文件,命名成为app.config。然后用System.Configuration.ConfigurationManager就可以了。
例如我在处理连接数据库的连接字符串就可以采用这样的方法
在app.config中编写下列代码
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="SqlConnectionString"
connectionString="Data Source=.;Initial Catalog=HotelManager;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="dbType" value="Sql" />
</appSettings>
</configuration>

在访问数据库的类中可以这样调用
using System.Configuration;

 string conString =
ConfigurationManager.ConnectionStrings["SqlConnectionString"].ToString();







------解决方案--------------------
写和读的规则一致就行了
------解决方案--------------------
1.config文件
2.读文件的方式。
3.还有一种是用类的反序列化原理来做。
using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

/* The XmlRootAttribute allows you to set an alternate name 
(PurchaseOrder) of the XML element, the element namespace; by 
default, the XmlSerializer uses the class name. The attribute 
also allows you to set the XML namespace for the element. Lastly,
the attribute sets the IsNullable property, which specifies whether 
the xsi:null attribute appears if the class instance is set to 
a null reference. */
[XmlRootAttribute("PurchaseOrder", Namespace="http://www.cpandl.com", 
IsNullable = false)]
public class PurchaseOrder
{
public Address ShipTo;
public string OrderDate; 
/* The XmlArrayAttribute changes the XML element name
from the default of "OrderedItems" to "Items". */
[XmlArrayAttribute("Items")]
public OrderedItem[] OrderedItems;
public decimal SubTotal;
public decimal ShipCost;
public decimal TotalCost;
}
 
public class Address
{
/* The XmlAttribute instructs the XmlSerializer to serialize the Name
field as an XML attribute instead of an XML element (the default
behavior). */
[XmlAttribute]
public string Name;
public string Line1;

/* Setting the IsNullable property to false instructs the 
XmlSerializer that the XML attribute will not appear if 
the City field is set to a null reference. */
[XmlElementAttribute(IsNullable = false)]
public string City;
public string State;
public string Zip;
}
 
public class OrderedItem
{
public string ItemName;
public string Description;
public decimal UnitPrice;
public int Quantity;
public decimal LineTotal;

/* Calculate is a custom method that calculates the price per item,
and stores the value in a field. */
public void Calculate()
{
LineTotal = UnitPrice * Quantity;
}
}
 
public class Test
{
public static void Main()
{
// Read and write purchase orders.
Test t = new Test();
t.CreatePO("po.xml");