C# INI 写入,读取,取得等操作

时间:2022-11-01 04:30:51

pre.ini 文件

;PRM1-患者编号NO
 [PRE_VALUE]
 PRM1="0000003"
 
 //写INI
private void cmd_Save_Click(object sender, EventArgs e)
  {
   try
   {
    if (!SetIniInfo("pre.ini", "PRE_VALUE", "PRM1", ComboBox.Text))
    {
     return;
    }
   }
   catch (Exception ex)
   {
    MessageBox.Show("INIファイルに書き込み失敗しました。\r\n" + ex.Message.ToString(), "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
   }
  }
//取得INI
  private void cmd_getIni_Click(object sender, EventArgs e)
  {
   string sTimeValue = "";
   GetIniAPI("pre.ini", "PRE_VALUE", "PRM1", ref lstr_Set5);
   //下面调用即可。
  }


 //*************************************************************************
        // @(f)
        // 技能    : pre.ini 系统信息取得
        // 返回值   : TRUE    - 正常終了
        //       : FALSE   - 異常終了
        // 参数     : 无
        // 技能説明  : pre.ini信息取得
        // 备考    :
        //*************************************************************************
        public static bool GetIniInfo()
        {
            bool returnValue;
            string strGetItem = "";
            returnValue = false;

            //'PRM1取得
            if (gbFNC_GetIniAPI("pre.ini", "PRE_VALUE", "PRM1",ref strGetItem) != true)
            {
                return returnValue;
            }
            string PRM1 = strGetItem;
            returnValue = true;
            return returnValue;
       }

        [DllImport("kernel32")]
        public static extern int WritePrivateProfileString(string section, string key,
                                                             string val, string filePath);
  [DllImport("kernel32")]
        public static extern int GetPrivateProfileString(string section, string key,
                                                          string def, StringBuilder retVal,
                                                          int size, string filePath);

  //*************************************************************************
  // @(f)
  // 技能    : pre.ini信息写入
  // 返回值   : TRUE  - 正常終了
  //       : FALSE - 異常終了
  // 参数     : wFileName    - 文件名
  //       : wSection     - ini节点名
  //       : wEntryName   - 输入的名
  //       : wSetting     - 写入的信息
  // 技能説明  : 系统函数「WritePrivateProfileString」によりpre.INI
  //       :Key(AppName、KeyName)等(wInfo)信息写入。
  // 备考    :
  //*************************************************************************

 public static bool SetIniInfo(string wFileName, string wSection, string wEntryName, string wSetting)
  {
   string lstr_IniFName = "";
   int llng_Ret;

            lstr_IniFName = Application.StartupPath + "\\" + wFileName;
            llng_Ret = Win32.WritePrivateProfileString(wSection, wEntryName, '\u0022' + wSetting + '\u0022', lstr_IniFName);
            return llng_Ret > 0;
  }


       //*************************************************************************
        // @(f)
        // 技能    : pre.ini指定的节点取得
        // 返回值   : True - 正常終了
        //       : False - 異常終了
     // 参数      : wFileName    - 文件名
   //       : wSection     - ini节点名
   //       : wEntryName   - 输入的名
        //       : wGetItem     - 取得节点值
        // 技能説明  : 系统函数「GetPrivateProfileString」によりpre.INIから
        //       : wSectionName,wEntryName信息取得。
        // 备考    : ini文件在实行环境下的使用する。
        //*************************************************************************
  public static bool GetIniAPI(string wFileName, string wSectionName, string wEntryName, ref string wGetItem)
        {

            bool returnValue = false;
            string strIniFName = "";
            StringBuilder strKeyName = new StringBuilder();
            string strValueKeyName = "";
            int lngRet;
            try
            {
                wGetItem = "";
                //Pathを設定
                strIniFName = Application.StartupPath + "\\" + wFileName;
                strKeyName.Capacity = 500;
                strValueKeyName = Strings.Space(500);
                //値を取得
                lngRet = Win32.GetPrivateProfileString(wSectionName, wEntryName, strValueKeyName, strKeyName, strKeyName.Capacity, strIniFName);
                string sTemp = strKeyName.ToString();
                wGetItem = sTemp;
                returnValue = true;
                return returnValue;
            }
            catch (Exception)
            {
                return returnValue;
            }
        }

  编辑

public static void EditIniFile(int curLine, string newLineValue, string patch)
        {
            FileStream fs = new FileStream(patch, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("Shift_JIS"));
            string line = sr.ReadLine();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; line != null; i++)
            {
                sb.Append(line + "\r\n");
                if (i != curLine - 1)
                    line = sr.ReadLine();
                else
                {
                    sr.ReadLine();
                    line = newLineValue;
                }
            }
            sr.Close();
            fs.Close();
            FileStream fs1 = new FileStream(patch, FileMode.Open, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs1, Encoding.GetEncoding("Shift_JIS"));
            sw.Write(sb.ToString());
            sw.Close();
            fs.Close();
        }