黄色电影一区二区,韩国少妇自慰A片免费看,精品人妻少妇一级毛片免费蜜桃AV按摩师 ,超碰 香蕉

C#?讀寫編輯INI文件的操作


c# 讀寫編輯ini文件

ini文件概念

ini就是擴(kuò)展名為"ini"的文件,其實(shí)他本身是個文本文件,可以用記事本打開,主要存放的是用戶所做的選擇或系統(tǒng)的各種參數(shù)。

c#讀寫ini文件其實(shí)并不是普通的文本文件.它有自己的結(jié)構(gòu).由若干段落(section)組成,在每個帶括號的標(biāo)題下面,是若干個以單個單詞開頭的關(guān)鍵字(keyword)和一個等號,等號右邊就是關(guān)鍵字的值(value),例如,

[section1]

keyword1 = value1 
keyword2 = value2 
... 

[section2]

keyword3 = value3 
keyword4 = value4

ini文件讀操作

c#命名空間中沒有直接讀寫ini的類,雖然c#中沒有,但是在"kernel32.dll"這個文件中有win32的api函數(shù)–writeprivateprofilestring()和getprivateprofilestring()

類文件

using system;
using system.collections.generic;
using system.io;
using system.linq;
using system.text;
namespace files
{
  class filesini
  {
      // 聲明ini文件的寫操作函數(shù) writeprivateprofilestring()
      [system.runtime.interopservices.dllimport("kernel32")]
      private static extern long writeprivateprofilestring(string section, string key, string val, string filepath);
      // 聲明ini文件的讀操作函數(shù) getprivateprofilestring()
      [system.runtime.interopservices.dllimport("kernel32")]
      private static extern int getprivateprofilestring(string section, string key, string def, system.text.stringbuilder retval, int size, string filepath);
      /// 寫入ini的方法
      public void iniwrite(string section, string key, string value,string path)
      {
          // section=配置節(jié)點(diǎn)名稱,key=鍵名,value=返回鍵值,path=路徑
          writeprivateprofilestring(section, key, value, path);
      }
      //讀取ini的方法
      public string iniread(string section, string key,string path)
      {
          // 每次從ini中讀取多少字節(jié)
          system.text.stringbuilder temp = new system.text.stringbuilder(255);
          // section=配置節(jié)點(diǎn)名稱,key=鍵名,temp=上面,path=路徑
          getprivateprofilestring(section, key, "", temp, 255, path);
          return temp.tostring();
      }
      //刪除一個ini文件
      public void inidelete(string filepath)
      {
          file.delete(filepath);
      }
  }
}

主函數(shù)

using files;
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.threading.tasks;
using system.windows.forms;
namespace ini文件讀寫操作
{
  public partial class form1 : form
  {
      public form1()
      {
          initializecomponent();
      }
      string ip;
      int port;
      int speed;
      //聲明默認(rèn)配置文件路徑
      public string inipath = convert.tostring(system.appdomain.currentdomain.basedirectory) + "config.ini";
      private void form1_load(object sender, eventargs e)
      {
          filesini configini = new filesini();
          ip = configini.iniread("fanuc機(jī)器人控制參數(shù)", "ip", inipath);
          port = convert.toint32(configini.iniread("fanuc機(jī)器人控制參數(shù)", "port", inipath));
          speed = convert.toint32(configini.iniread("fanuc機(jī)器人控制參數(shù)", "speed", inipath));
          textbox1.text = ip;
          textbox2.text = port.tostring();
          textbox3.text = speed.tostring();
      }
  }
}

ini文件修改操作

修改ip

 private void button1_click(object sender, eventargs e)
      {
          filesini configini = new filesini();
          configini.iniwrite("fanuc機(jī)器人控制參數(shù)", "ip", textbox1.text, inipath);
      }

ini文件寫操作

添加新的配置節(jié)點(diǎn)【kuka機(jī)器人控制參數(shù)】

 private void button1_click(object sender, eventargs e)
      {
          filesini configini = new filesini();
          configini.iniwrite("kuka機(jī)器人控制參數(shù)", "ip", textbox1.text, inipath);
      }

在某個配置節(jié)點(diǎn)下,添加新的關(guān)鍵字angle

private void button1_click(object sender, eventargs e)
      {
          filesini configini = new filesini();
          configini.iniwrite("kuka機(jī)器人控制參數(shù)", "angle", textbox1.text, inipath);
      }

 

c#讀寫ini文件案例

提示:以下是本篇文章正文內(nèi)容,下面案例可供參考

一、命名空間

首先需要引用命名空間命名空間
using system.runtime.interopservices;

二、函數(shù)封裝

代碼如下(示例):

        [dllimport("kernel32")]
      private static extern long writeprivateprofilestring(string section, string key, string val, string filepath);
      [dllimport("kernel32")]
      private static extern long getprivateprofilestring(string section, string key, string def, stringbuilder retval, int size, string filepath);
      #region   創(chuàng)建文件
      public static void createfile(string path)
      {
          if (!string.isnullorempty(path))
          {
              try
              {
                  string dr = path.getdirectoryname(path);
                  if (!directory.exists(dr))
                  {
                      directory.createdirectory(dr);
                  }
                  if (!file.exists(path))
                  {
                      filestream fs = file.create(path);
                      fs.close();
                  }
              }
              catch (exception e)
              {
              }
          }
      }
      #endregion
      #region 寫ini文件
      ///ini文件中的節(jié)名
      ///ini 文件中的健
      ///要寫入該健所對應(yīng)的值
      ///ini文件路徑
      public static bool writeinidata(string section, string key, string val, string inifilepath)
      {
          if (file.exists(inifilepath))
          {
              long opst = writeprivateprofilestring(section, key, val, inifilepath);
              if (opst == 0)
              {
                  return false;
              }
              else
              {
                  return true;
              }
          }
          else
          {
              createfile(inifilepath);
              long opst = writeprivateprofilestring(section, key, val, inifilepath);
              if (opst == 0)
              {
                  return false;
              }
              else
              {
                  return true;
              }
          }
      }
      #endregion
      #region  取ini文件
      /// 節(jié)點(diǎn)名稱
      /// 對應(yīng)的key
      /// 讀不到值時返回的默認(rèn)值
      /// 文件路徑
      public static string readinidata(string section, string key, string notext, string inifilepath)
      {
          if (file.exists(inifilepath))
          {
              stringbuilder temp = new stringbuilder(1024);
              long k = getprivateprofilestring(section, key, notext, temp, 1024, inifilepath);
              if (k != 0)
              {
                  return temp.tostring();
              }
              else
              {
                  return string.empty;
              }
          }
          else
          {
              return string.empty;
          }
      }
      #endregion

三、數(shù)據(jù)讀寫

代碼如下(示例):

#region   把key——value寫入ini文件
      public bool savepwdtoini(string pwd)
      {
          string path = system.appdomain.currentdomain.basedirectory;
          path += "\\" + "ini" + "\\password.ini";
          bool b = writeinidata("section_1","pwd",pwd,path);
          return b;
      }
#endregion
#region   從路徑下的ini文件讀取key對應(yīng)的value
      public string readpwdfromini()
      {
          string path = system.appdomain.currentdomain.basedirectory;
          path += "\\" + "ini" + "\\password.ini";
          string s = readinidata("section_1","pwd","error",path);
          return s;
      }
		//數(shù)據(jù)讀寫
		bool b = writeinidata("section_1", "key", value, path);
      string s = readinidata("section_2", "key", "error", path);

以上內(nèi)容簡單介紹了c#里面ini文件的讀寫,封裝后的函數(shù)可以大幅度地提高編程效率。

相關(guān)文章