資源簡介
這是一個通用的讀寫INI文件的類,直接加入到您的程序中,引用后直接使用,其中有兩個函數,一個讀取配置文件中指定的配置的值,一個將指定的值寫入到配置文件中。其中有配置文件路徑,默認是在程序目錄中,有特殊需要的可以修改。從此操作配置文件將非常方便。
代碼片段和文件信息
using?System;
using?System.IO;
using?System.xml;
using?System.Data;
using?System.Text;
using?System.Drawing;
using?System.Collections;
using?System.Windows.Forms;
using?System.Reflection;
using?System.Runtime.InteropServices;
using?System.ComponentModel;
namespace?gpsserver
{
????class?INI
????{
????????///?
????????///?申明ini文件的寫操作函數WritePrivateProfileString()
????????///?
????????///?
????????///?
????????///?
????????///?
????????///?
????????[DllImport(“Kernel32.dll“)]
????????public?static?extern?int?WritePrivateProfileString(string?section?string?key?string?val?string?filepath);
????????///?
????????///申明ini文件的讀操作函數GetPrivateProfileString()
????????///?
????????///?
????????///?
????????///?
????????///?
????????///?
????????///?
????????///?
????????[DllImport(“Kernel32.dll“)]
????????public?static?extern?int?GetPrivateProfileString(string?section?string?key?string?def?System.Text.StringBuilder?retVal?int?size?string?filePath);
????????///?
????????///?讀取配置文件信息
????????///?
????????///?
????????///?
????????///?
????????public?static?string?Read_INI(string?s1?string?s2)
????????{
????????????try
????????????{
????????????????string?iniPath?=?Application.StartupPath?+?@“\Config.ini“;
????????????????StringBuilder?sbudtemp?=?new?StringBuilder(255);
????????????????GetPrivateProfileString(s1?s2?“error“?sbudtemp?255?iniPath);
????????????????return?sbudtemp.ToString().Trim();
????????????}
????????????catch
????????????{
????????????????return?string.Empty;
????????????}
????????}
????????///?
????????///?信息寫入配置文件
????????///?
????????///?
????????///?
????????///?
????????///?
????????public?static?bool?Write_INI(string?s1?string?s2?string?s3)
????????{
????????????try
????????????{
????????????????string?iniPath?=?Application.StartupPath?+?@“\Config.ini“;
????????????????WritePrivateProfileString(s1?s2?s3?iniPath);
????????????????return?true;
????????????}
????????????catch
????????????{
????????????????return?false;
????????????}
????????}
????}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????2697??2007-11-27?14:16??INI.cs
-----------?---------??----------?-----??----
?????????????????2697????????????????????1
評論
共有 條評論