中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

C#進行INI文件的讀寫操作

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用

VC中提供了API函數進行INI文件的讀寫操作,但是微軟推出的C#編程語言中卻沒有相應的方法,下面是一個C# ini文件讀寫類,從網上收集的,很全,就是沒有對section的改名功能。

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
 
namespace wuyisky{
  /**//**/
  /**//// <summary>
  /// IniFiles的類
  /// </summary>
  public class IniFiles
  {
    public string FileName; //INI文件名
    //聲明讀寫INI文件的API函數
    [DllImport("kernel32")]
    private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath);
    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);
    //類的構造函數,傳遞INI文件名
    public IniFiles(string AFileName)
    {
      // 判斷文件是否存在
      FileInfo fileInfo = new FileInfo(AFileName);
      //Todo:搞清枚舉的用法
      if ((!fileInfo.Exists))
      { //|| (FileAttributes.Directory in fileInfo.Attributes))
        //文件不存在,建立文件
        System.IO.StreamWriter sw = new System.IO.StreamWriter(AFileName, false, System.Text.Encoding.Default);
        try
        {
          sw.Write("#表格配置檔案");
          sw.Close();
        }
 
        catch
        {
          throw (new ApplicationException("Ini文件不存在"));
        }
      }
      //必須是完全路徑,不能是相對路徑
      FileName = fileInfo.FullName;
    }
    //寫INI文件
    public void WriteString(string Section, string Ident, string Value)
    {
      if (!WritePrivateProfileString(Section, Ident, Value, FileName))
      {
 
        throw (new ApplicationException("寫Ini文件出錯"));
      }
    }
    //讀取INI文件指定
    public string ReadString(string Section, string Ident, string Default)
    {
      Byte[] Buffer = new Byte[65535];
      int bufLen = GetPrivateProfileString(Section, Ident, Default, Buffer, Buffer.GetUpperBound(0), FileName);
      //必須設定0(系統(tǒng)默認的

標簽:

版權申明:本站文章部分自網絡,如有侵權,請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:C# 獲取內存和CPU信息的代碼

下一篇:C# 生成隨機數的代碼片段