代码如下
 public string FileName; //INI文件名
        //声明读写INI文件的API函数
        /// <summary>
        /// 
        /// </summary>
        /// <param name="section">指向包含 Section 名称的字符串</param>
        /// <param name="key">指向包含 Key 名称的字符串</param>
        /// <param name="val">要写的字符串</param>
        /// <param name="filePath">ini 文件的文件名</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath);
        /// <summary>
        /// 从 ini 文件的某个 Section 取得一个 key 的字符串
        /// </summary>
        /// <param name="section">指向包含 Section 名称的字符串</param>
        /// <param name="key">指向包含 Key 名称的字符串</param>
        /// <param name="def">如果 Key 值没有找到,则返回缺省的字符串</param>
        /// <param name="retVal">返回字符串的缓冲区</param>
        /// <param name="size">缓冲区的长度</param>
        /// <param name="filePath">ini 文件的文件名</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);        //类的构造函数,传递INI文件名
        public IniFileRead(string AFileName)
        {
            // 判断文件是否存在
            FileInfo fileInfo = new FileInfo(AFileName);
            //Todo:搞清枚举的用法
            if ((!fileInfo.Exists))
            {
                //文件不存在,建立文件
                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文件指定
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Section"></param>
        /// <param name="Ident"></param>
        /// <param name="Default"></param>
        /// <returns></returns>
        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(系统默认的代码页)的编码方式,否则无法支持中文
            string s = Encoding.GetEncoding(0).GetString(Buffer);
            s = s.Substring(0, bufLen);
            return s.Trim();
        }        //读整数
        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;
            }
        }
        
        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;
                    }
                }
            }
        }
      
        //读取指定的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下的某个键值是否存在
        public bool ValueExists(string Section, string Ident)
        {
            //
            StringCollection Idents = new StringCollection();
            ReadSection(Section, Idents);
            return Idents.IndexOf(Ident) > -1;
        }
        //确保资源的释放
        ~IniFileRead()
        {
            UpdateFile();
        }
    }
在窗体里面调用
IniFileRead inifile = new IniFileRead("e:\\c.ini");
//第三个参数20是默认值,就是如果读取失败则返回默认值
string value = inifile.ReadString("rectangle", "width", "20");ini文件格式
[rectangle]
width=1024
height=768
bit=32
refush=60现在的问题是我读取不到值,按照正常的应该是能获取到1024
但是我用这个方法读C盘下的BOOT.ini是可以取到里面的值的,所以代码应该没什么问题
是否是我的ini文件有问题

解决方案 »

  1.   


    private string m_iniPath;  ////声明读写INI文件的API函数 
    [DllImport("kernel32")]  private static extern long WritePrivateProfileString(string section,string key,string val,string filePath); 
    [DllImport("kernel32")]  private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath);  public clsIni(string iniPath)
    {
    m_iniPath = iniPath; 
    } //写INI文件 
    public void IniWriteValue(string Section,string Key,string Value) 

    WritePrivateProfileString(Section,Key,Value,this.m_iniPath); 
    }  //读取INI文件指定 
    public string IniReadValue(string Section,string Key) 

    StringBuilder temp = new StringBuilder(255); 
    int i = GetPrivateProfileString(Section,Key,"",temp,255,this.m_iniPath); 
    return temp.ToString(); 
    }
    这个就可以了,不用你写的那么复杂吧!
      

  2.   

    我刚才又试了试
    发现我把C盘的boot.ini文件复制到E盘,然后把里面东西换成我自己的并且把文件重命名为
    b.ini就能读出来
    难道ini文件有特殊的结构,普通的txt文件改后缀名不行?
      

  3.   

    我读INI和读TXT一样  我的方法比较简陋 呵呵
    加入int中有一行 ServerIP=127.0.0.1StreamReader srd=File.OpenText("xxx.ini");
    string[] str_tempAraay=srd.ReadLine().Split('=');
    if(str_tempAraay[0]=="Server")
    {MessageBox.Show(str_tempAraay[1]);}
      

  4.   

    .....这么用的话我还不如直接用XML
      

  5.   

    如果是你现在这样情况的话,只能找些例子测试下了;
    我的就用程序生成的ini文件,没有txt直接改这么用过呢
      

  6.   

    ...问题解决
    应该是编码的问题
    AFileName="路径";
    System.IO.StreamWriter sw = new System.IO.StreamWriter(AFileName, false, System.Text.Encoding.Default);
    用这个方法生成一个空的ini文件,然后再读就可以了
    估计System.Text.Encoding.Default是重点
      

  7.   

    就是通过程序来创建INI文件,这样吧,你给我个邮箱,我晚上回家了给你传个例子源代码吧;
    我想在想起来了,我以前做服务的时候也是直接创建个txt文件,然后改下扩展名就直接用了,也没有出现过你说的问题