• <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

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

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

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

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

                   INI文件是文本文件,由若干節(jié)(section)組成,在每個帶括號的標題下面,是若干個關(guān)鍵詞(key)及其對應的值(Value)

              [Section]

              Key=Value

                  

                   VC中提供了API函數(shù)進行INI文件的讀寫操作,但是微軟推出的C#編程語言中卻沒有相應的方法,下面是一個C# ini文件讀寫類,從網(wǎng)上收集的,很全,就是沒有對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函數(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文件不存在"));
                    }

                  }

                  
            //必須是完全路徑,不能是相對路徑
                  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);
                  
            //必須設(shè)定0(系統(tǒng)默認的代碼頁)的編碼方式,否則無法支持中文
                  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);
                  
            //對Section進行解析
                  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來實現(xiàn),StringBuilder只能取到第一個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());
             
                
            //  }
                
            //}
                
            //清除某個Section
                public void EraseSection(string Section)
                
            {
                  
            //
                  if (!WritePrivateProfileString(Section, nullnull, FileName))
                  
            {

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

                }

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

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


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


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

              }

            }


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

            評論

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

            # re: C#讀寫INI文件 2008-05-23 15:18 lingyuan
            恩,很好,謝謝  回復  更多評論
              

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

            # re: C#讀寫INI文件 2009-03-02 17:32 liyi
            你能再蠢一點嗎?@123
              回復  更多評論
              

            # re: C#讀寫INI文件 2009-03-02 17:32 liyi
            lingyuan
            你能再蠢一點嗎?@123   回復  更多評論
              

            # re: C#讀寫INI文件 2009-05-18 12:41 AAA
            樓主你能再蠢一點嗎?@  回復  更多評論
              

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

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

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

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

            # re: C#讀寫INI文件 2010-01-29 12:32 c#lover
            我是感覺用xml有格式限制。比如存一段html文件,就用問題了。 @我們學習網(wǎ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.   回復  更多評論
              

            # 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.   回復  更多評論
              

            # 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.   回復  更多評論
              

            # re: C#讀寫INI文件 2010-07-05 22:36 華子
            怎么還回一個DataTable啊?  回復  更多評論
              

            久久久老熟女一区二区三区| 日本免费一区二区久久人人澡| 蜜桃麻豆WWW久久囤产精品| 亚洲女久久久噜噜噜熟女| 日本精品久久久久中文字幕| 欧美激情精品久久久久久| 久久超碰97人人做人人爱| 日韩电影久久久被窝网| 国产日产久久高清欧美一区| 一个色综合久久| 色综合色天天久久婷婷基地| 亚洲级αV无码毛片久久精品| 中文字幕亚洲综合久久| 久久夜色精品国产噜噜亚洲AV | 777午夜精品久久av蜜臀| 青青草原1769久久免费播放| 中文精品久久久久人妻不卡| 久久精品国产精品亚洲人人| 99久久精品日本一区二区免费| 久久午夜无码鲁丝片秋霞| 久久久久国产一区二区| 久久国产精品久久久| 麻豆精品久久久久久久99蜜桃| 久久久久亚洲爆乳少妇无| 久久精品国产只有精品2020| 精品久久久久久久久午夜福利| 亚洲人成无码www久久久| 久久久WWW成人免费精品| 精品久久久久久久久中文字幕| 久久久噜噜噜久久中文福利| 久久精品国产99国产精品亚洲| 欧美激情精品久久久久久| 国内精品伊人久久久久影院对白| 99久久免费国产精品热| 97久久香蕉国产线看观看| 国产精品禁18久久久夂久| 亚洲AV日韩精品久久久久久久| 久久久久亚洲AV无码观看| 日本高清无卡码一区二区久久| 久久综合久久伊人| 伊人色综合久久天天网|