一、什么是C#配置文件
C#配置文件是用于在应用程序运行时存储数据和配置设置的文本文件。它通常包含一组键值对,可以通过程序访问。
常见的C#配置文件有两种格式:App.config和Web.config。前者用于Windows应用程序,后者用于Web应用程序。它们都使用XML格式存储配置信息。
二、C#配置文件的作用
C#配置文件的作用是分离程序逻辑与配置数据,使得我们可以在不修改代码的情况下修改应用程序的行为。
除此之外,C#配置文件还可以用于存储应用程序需要的各种信息,比如数据库连接字符串、邮件服务器信息、日志记录选项等等。
三、C#配置文件的使用方法
C#配置文件可以通过System.Configuration命名空间提供的ConfigurationManager类来访问。下面是一个从App.config中读取配置信息并输出到控制台的例子:
using System.Configuration; using System; namespace ConfigFileExample { class Program { static void Main(string[] args) { string message = ConfigurationManager.AppSettings["Message"]; int level = Int32.Parse(ConfigurationManager.AppSettings["LogLevel"]); Console.WriteLine("{0} (Log level: {1})", message, level); } } }
上面的代码中,ConfigurationManager.AppSettings[“Message”]可以读取App.config文件中key为Message的配置项的值,ConfigurationManager.AppSettings[“LogLevel”]可以读取key为LogLevel的配置项的值。
四、C#配置文件的语法规则
C#配置文件使用XML格式存储数据,因此遵循XML的语法规则。比如,每个配置项应该以<add>标签定义,且必须有一个name和value属性。如果有多个配置项,应该使用<appSettings>标签将它们包裹起来。
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="Message" value="Hello, World!" /> <add key="LogLevel" value="1" /> </appSettings> </configuration>
五、C#配置文件的案例
1、从配置文件中读取数据库连接字符串
<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="MyDB" connectionString="server=localhost;database=mydb;uid=myuser;pwd=mypassword" /> </connectionStrings> </configuration>
using System.Configuration; using System.Data.SqlClient; namespace ConfigFileExample { class Program { static void Main(string[] args) { string connectionString = ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand("SELECT COUNT(*) FROM MyTable", connection); connection.Open(); int count = (int)command.ExecuteScalar(); Console.WriteLine("Total rows: {0}", count); } } } }
2、从配置文件中读取日志级别
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="LogLevel" value="2" /> </appSettings> </configuration>
using System.Configuration; using System; namespace ConfigFileExample { class Program { static void Main(string[] args) { int logLevel = Int32.Parse(ConfigurationManager.AppSettings["LogLevel"]); if (logLevel >= 2) { Console.WriteLine("Warning: Something bad might happen!"); } } } }
3、从配置文件中读取邮件服务器信息
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="SmtpServer" value="smtp.example.com" /> <add key="SmtpPort" value="587" /> </appSettings> </configuration>
using System.Configuration; using System.Net; using System.Net.Mail; using System; namespace ConfigFileExample { class Program { static void Main(string[] args) { string smtpServer = ConfigurationManager.AppSettings["SmtpServer"]; int smtpPort = Int32.Parse(ConfigurationManager.AppSettings["SmtpPort"]); using (SmtpClient client = new SmtpClient(smtpServer, smtpPort)) { MailMessage message = new MailMessage("from@example.com", "to@example.com", "Hello from C#!", "This is a test message."); client.Send(message); } Console.WriteLine("Message has been sent."); } } }
原创文章,作者:KQLI,如若转载,请注明出处:https://www.506064.com/n/138609.html