一、什麼是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/zh-hk/n/138609.html