INI格式檔案操作

來源:互聯網
上載者:User

        晚上忙著忙著,不知道自己在忙些啥,看看這看看那的,新進一家公司,做的東西,以前都沒有接觸過的,上班的時候需要用到INI檔案來記錄一些資訊。我這個呢是沒有加密的,當然加一個加密的,或者轉成位元組都可以,這樣子就安全點。隨便整理下這些老生常談的東西,部落格園也有例子。具體網址我不記得了,上班的時候看的。貼點代碼,更新下部落格。

下面是INIFileHepler類

  /// <summary>
/// 寫入INI檔案
/// </summary>
/// <param name="section">節點名稱[如[TypeName]]</param>
/// <param name="key">鍵</param>
/// <param name="val">值</param>
/// <param name="filepath">檔案路徑</param>
/// <returns></returns>
[DllImport("kernel32")]
public static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
/// <summary>
/// 讀取INI檔案
/// </summary>
/// <param name="section">節點名稱</param>
/// <param name="key">鍵</param>
/// <param name="def">值</param>
/// <param name="retval">stringbulider對象</param>
/// <param name="size">位元組大小</param>
/// <param name="filePath">檔案路徑</param>
/// <returns></returns>
[DllImport("kernel32")]
public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);

記得匯入命名空間:

using System.Runtime.InteropServices;

然後是操作類FileManage

 public class FileManage
{

public FileManage(string path)
{
strFilePath = path;
}
private string strSec = ""; //INI檔案名稱
private string strFilePath;
/// <summary>
/// 寫入
/// </summary>
/// <param name="key">寫入key</param>
/// <param name="value">寫入value</param>
public void Write(string key, string value)
{
try
{

//根據INI檔案名稱設定要寫入INI檔案的節點名稱
//此處的節點名稱完全可以根據實際需要進行配置
strSec = Path.GetFileNameWithoutExtension(strFilePath);
INIFileHepler.WritePrivateProfileString(strSec, key, value, strFilePath);

}
catch
{
throw new Exception("設定檔不存在或許可權不足導致無法寫入");
}
}
/// <summary>
/// 讀取
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public string Read(string key)
{
if (File.Exists(strFilePath))//讀取時先要判讀INI檔案是否存在
{

strSec = Path.GetFileNameWithoutExtension(strFilePath);
return ContentValue(strSec, key);


}
else
{

throw new Exception("設定檔不存在");

}
}
/// <summary>
/// 自訂讀取INI檔案中的內容方法
/// </summary>
/// <param name="Section">鍵</param>
/// <param name="key">值</param>
/// <returns></returns>
private string ContentValue(string Section, string key)
{

StringBuilder temp = new StringBuilder(1024);
INIFileHepler.GetPrivateProfileString(Section, key, "", temp, 1024, strFilePath);
return temp.ToString();
}
}

有這些最後就是調用的問題啦。。

private string strFilePath = Application.StartupPath + "\\FileConfig.ini";//擷取INI檔案路徑

//寫入
private void button1_Click(object sender, EventArgs e)
{
FileManage file = new FileManage(strFilePath);
file.Write(label1.Text, textBox1.Text);
file.Write(label2.Text, textBox2.Text);
MessageBox.Show("寫入完畢");
}
//讀取
private void button2_Click(object sender, EventArgs e)
{
FileManage file = new FileManage(strFilePath);
StringBuilder str = new StringBuilder();
str.AppendLine(file.Read(label1.Text));
str.AppendLine(file.Read(label2.Text));
MessageBox.Show(str.ToString());
}

看看運行效果:

很簡單的小玩意。。但是作用也很大。。比如自動登入功能,記錄配置資訊啥的。。挺方便的。。當然這個也可以選擇xml來記錄的。。這個東西看個人喜好了。

夜深了。。又是一夜。。明天繼續折騰。 

______________________________________________________________________________

 源碼:

INI檔案操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.