• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            twzheng's cppblog

            『站在風(fēng)口浪尖緊握住鼠標(biāo)旋轉(zhuǎn)!』 http://www.cnblogs.com/twzheng

              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              136 隨筆 :: 78 文章 :: 353 評(píng)論 :: 0 Trackbacks
            C#讀寫INI文件 

            摘自:伊圖教程網(wǎng)[www.etoow.com]
            http://www.etoow.com/html/2007-08/1187271505-1.html
             

                   雖然微軟早已經(jīng)建議在
            WINDOWS中用注冊(cè)表代替INI文件,但是在實(shí)際應(yīng)用中,INI文件仍然有用武之地,尤其現(xiàn)在綠色軟件的流行,越來越多的程序?qū)⒆约旱囊恍┡渲眯畔⒈4娴搅?/span>INI文件中。

                   INI文件是文本文件,由若干節(jié)(section)組成,在每個(gè)帶括號(hào)的標(biāo)題下面,是若干個(gè)關(guān)鍵詞(key)及其對(duì)應(yīng)的值(Value)

              [Section]

              Key=Value

                  

                   VC中提供了API函數(shù)進(jìn)行INI文件的讀寫操作,但是微軟推出的C#編程語言中卻沒有相應(yīng)的方法,下面是一個(gè)C# ini文件讀寫類,從網(wǎng)上收集的,很全,就是沒有對(duì)section的改名功能,高手可以增加一個(gè)。

            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函數(shù)
                [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);
                
            //類的構(gòu)造函數(shù),傳遞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文件不存在"));
                    }

                  }

                  
            //必須是完全路徑,不能是相對(duì)路徑
                  FileName = fileInfo.FullName;
                }

                
            //寫INI文件
                public void WriteString(string Section, string Ident, string Value)
                
            {
                  
            if (!WritePrivateProfileString(Section, Ident, Value, FileName))
                  
            {
             
                    
            throw (new ApplicationException("寫Ini文件出錯(cuò)"));
                  }

                }

                
            //讀取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);
                  
            //必須設(shè)定0(系統(tǒng)默認(rèn)的代碼頁)的編碼方式,否則無法支持中文
                  string s = Encoding.GetEncoding(0).GetString(Buffer);
                  s 
            = s.Substring(0, bufLen);
                  
            return s.Trim();
                }


                
            //讀整數(shù)
                public int ReadInteger(string Section, string Ident, int Default)
                
            {
                  
            string intStr = ReadString(Section, Ident, Convert.ToString(Default));
                  
            try
                  
            {
                    
            return Convert.ToInt32(intStr);

                  }

                  
            catch (Exception ex)
                  
            {
                    Console.WriteLine(ex.Message);
                    
            return Default;
                  }

                }


                
            //寫整數(shù)
                public void WriteInteger(string Section, string Ident, int Value)
                
            {
                  WriteString(Section, Ident, Value.ToString());
                }


                
            //讀布爾
                public bool ReadBool(string Section, string Ident, bool Default)
                
            {
                  
            try
                  
            {
                    
            return Convert.ToBoolean(ReadString(Section, Ident, Convert.ToString(Default)));
                  }

                  
            catch (Exception ex)
                  
            {
                    Console.WriteLine(ex.Message);
                    
            return Default;
                  }

                }


                
            //寫B(tài)ool
                public void WriteBool(string Section, string Ident, bool Value)
                
            {
                  WriteString(Section, Ident, Convert.ToString(Value));
                }


                
            //從Ini文件中,將指定的Section名稱中的所有Ident添加到列表中
                public void ReadSection(string Section, StringCollection Idents)
                
            {
                  Byte[] Buffer 
            = new Byte[16384];
                  
            //Idents.Clear();

                  
            int bufLen = GetPrivateProfileString(Section, nullnull, Buffer, Buffer.GetUpperBound(0),
                   FileName);
                  
            //對(duì)Section進(jìn)行解析
                  GetStringsFromBuffer(Buffer, bufLen, Idents);
                }


                
            private void GetStringsFromBuffer(Byte[] Buffer, int bufLen, StringCollection Strings)
                
            {
                  Strings.Clear();
                  
            if (bufLen != 0)
                  
            {
                    
            int start = 0;
                    
            for (int i = 0; i < bufLen; i++)
                    
            {
                      
            if ((Buffer[i] == 0&& ((i - start) > 0))
                      
            {
                        String s 
            = Encoding.GetEncoding(0).GetString(Buffer, start, i - start);
                        Strings.Add(s);
                        start 
            = i + 1;
                      }

                    }

                  }

                }

                
            //從Ini文件中,讀取所有的Sections的名稱
                public void ReadSections(StringCollection SectionList)
                
            {
                  
            //Note:必須得用Bytes來實(shí)現(xiàn),StringBuilder只能取到第一個(gè)Section
                  byte[] Buffer = new byte[65535];
                  
            int bufLen = 0;
                  bufLen 
            = GetPrivateProfileString(nullnullnull, Buffer,
                   Buffer.GetUpperBound(
            0), FileName);
                  GetStringsFromBuffer(Buffer, bufLen, SectionList);
                }

                
            //讀取指定的Section的所有Value到列表中
                public void ReadSectionValues(string Section, NameValueCollection Values)
                
            {
                  StringCollection KeyList 
            = new StringCollection();
                  ReadSection(Section, KeyList);
                  Values.Clear();
                  
            foreach (string key in KeyList)
                  
            {
                    Values.Add(key, ReadString(Section, key, 
            ""));
              
                  }

                }

                
            ////讀取指定的Section的所有Value到列表中,
                //public void ReadSectionValues(string Section, NameValueCollection Values,char splitString)
                
            //{  string sectionValue;
                
            //  string[] sectionValueSplit;
                
            //  StringCollection KeyList = new StringCollection();
                
            //  ReadSection(Section, KeyList);
                
            //  Values.Clear();
                
            //  foreach (string key in KeyList)
                
            //  {
                
            //    sectionValue=ReadString(Section, key, "");
                
            //    sectionValueSplit=sectionValue.Split(splitString);
                
            //    Values.Add(key, sectionValueSplit[0].ToString(),sectionValueSplit[1].ToString());
             
                
            //  }
                
            //}
                
            //清除某個(gè)Section
                public void EraseSection(string Section)
                
            {
                  
            //
                  if (!WritePrivateProfileString(Section, nullnull, FileName))
                  
            {

                    
            throw (new ApplicationException("無法清除Ini文件中的Section"));
                  }

                }

                
            //刪除某個(gè)Section下的鍵
                public void DeleteKey(string Section, string Ident)
                
            {
                  WritePrivateProfileString(Section, Ident, 
            null, FileName);
                }

                
            //Note:對(duì)于Win9X,來說需要實(shí)現(xiàn)UpdateFile方法將緩沖中的數(shù)據(jù)寫入文件
                
            //在Win NT, 2000和XP上,都是直接寫文件,沒有緩沖,所以,無須實(shí)現(xiàn)UpdateFile
                
            //執(zhí)行完對(duì)Ini文件的修改之后,應(yīng)該調(diào)用本方法更新緩沖區(qū)。
                public void UpdateFile()
                
            {
                  WritePrivateProfileString(
            nullnullnull, FileName);
                }


                
            //檢查某個(gè)Section下的某個(gè)鍵值是否存在
                public bool ValueExists(string Section, string Ident)
                
            {
                  
            //
                  StringCollection Idents = new StringCollection();
                  ReadSection(Section, Idents);
                  
            return Idents.IndexOf(Ident) > -1;
                }


                
            //確保資源的釋放
                ~IniFiles()
                
            {
                  UpdateFile();
                }

              }

            }


            目前C# 對(duì)ini文件操作基本上要被xml文件取代了,但是我覺得ini文件的讀寫仍然是編程的基本,是必須會(huì)的
            posted on 2008-03-10 15:24 譚文政 閱讀(19861) 評(píng)論(15)  編輯 收藏 引用 所屬分類: windows 編程

            評(píng)論

            # re: C#讀寫INI文件 2008-03-18 09:38 123
            我還以為LZ要用I/O流來讀寫INI文件,看了才知道,還是API調(diào)用,就不要用“編程的基本”來形容了吧  回復(fù)  更多評(píng)論
              

            # re: C#讀寫INI文件 2008-05-23 15:18 lingyuan
            恩,很好,謝謝  回復(fù)  更多評(píng)論
              

            # re: C#讀寫INI文件 2008-08-03 18:08 lqh
            滿好的.有API非還要用IO流來讀寫INI.又不是做試驗(yàn),或說API不支持這種格式的讀寫.支持樓主.  回復(fù)  更多評(píng)論
              

            # re: C#讀寫INI文件 2009-03-02 17:32 liyi
            你能再蠢一點(diǎn)嗎?@123
              回復(fù)  更多評(píng)論
              

            # re: C#讀寫INI文件 2009-03-02 17:32 liyi
            lingyuan
            你能再蠢一點(diǎn)嗎?@123   回復(fù)  更多評(píng)論
              

            # re: C#讀寫INI文件 2009-05-18 12:41 AAA
            樓主你能再蠢一點(diǎn)嗎?@  回復(fù)  更多評(píng)論
              

            # re: C#讀寫INI文件 2009-08-19 16:58 Jod
            之前用IO自己寫了一個(gè),主要是用字符替換和正則匹配來做。
            這個(gè)方法和我看到的老外的源代碼差不多,也是一個(gè)方法。  回復(fù)  更多評(píng)論
              

            # re: C#讀寫INI文件 2009-08-28 13:54 weizy
            說樓主不好的自己貼一個(gè)看看,不要亂吆喝  回復(fù)  更多評(píng)論
              

            # re: C#讀寫INI文件 2009-09-09 09:24 劉永輝
            http://www.cnblogs.com/zzyyll2/archive/2007/11/06/950584.html  回復(fù)  更多評(píng)論
              

            # re: C#讀寫INI文件 2009-10-08 10:26 我們學(xué)習(xí)網(wǎng)
            呵呵,挺好的,不過我覺的用c#用xml挺好的,可以取代ini文件。  回復(fù)  更多評(píng)論
              

            # re: C#讀寫INI文件 2010-01-29 12:32 c#lover
            我是感覺用xml有格式限制。比如存一段html文件,就用問題了。 @我們學(xué)習(xí)網(wǎng)
              回復(fù)  更多評(píng)論
              

            # re: C#讀寫INI文件 2010-06-15 20:02 HaynesMadeline25
            Your useful data related to this topic goes parallel with the student dissertation. So, you could perform for <a href="http://www.master-dissertations.com">thesis writing</a> service.   回復(fù)  更多評(píng)論
              

            # re: C#讀寫INI文件 2010-06-15 23:49 term papers online
            Some specialists opine that to buy the analysis essays at the research paper services can be not a cheating, if college students study the papers closely.   回復(fù)  更多評(píng)論
              

            # re: C#讀寫INI文件 2010-06-18 11:44 thesis
            When students like to receive academic success on every subject you would order list of dissertation and buy thesis. I did that several times. I told just about dissertations in research paper writing services.   回復(fù)  更多評(píng)論
              

            # re: C#讀寫INI文件 2010-07-05 22:36 華子
            怎么還回一個(gè)DataTable啊?  回復(fù)  更多評(píng)論
              

            国产精品久久精品| 偷偷做久久久久网站| 国产成人无码精品久久久久免费| 国产韩国精品一区二区三区久久| 国产精品久久久久久久久免费| 国产精品久久久天天影视香蕉 | 久久亚洲私人国产精品vA| 久久精品国产清高在天天线| 久久久青草青青亚洲国产免观 | 久久66热人妻偷产精品9| 色综合久久中文色婷婷| 午夜精品久久久久成人| 精品永久久福利一区二区| 国产精品无码久久四虎| 久久精品国产精品亚洲精品| 久久国产精品久久国产精品| 中文字幕精品无码久久久久久3D日动漫| 久久精品国产亚洲AV蜜臀色欲| 国产精品一久久香蕉国产线看 | 欧美精品一区二区精品久久| 欧美无乱码久久久免费午夜一区二区三区中文字幕 | 国产成人精品久久亚洲| 久久久久久曰本AV免费免费| 久久久青草青青亚洲国产免观| 国产精品一区二区久久精品涩爱| AV狠狠色丁香婷婷综合久久| 伊人色综合九久久天天蜜桃| 免费国产99久久久香蕉| 久久亚洲春色中文字幕久久久 | 精品久久久久久久久久久久久久久 | 久久精品毛片免费观看| 伊人久久精品影院| 99热热久久这里只有精品68| 亚洲国产另类久久久精品| 久久国产精品波多野结衣AV| 97久久精品国产精品青草| 亚洲国产精品嫩草影院久久| 岛国搬运www久久| 91久久精一区二区三区大全| 香蕉久久夜色精品升级完成| 亚洲午夜无码AV毛片久久|