我用的是VS2005
在一些貼中看到這樣的寫法:
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace WindowsApplication2
...{
    /**//// <summary> 
    /// Summary description for Class1. 
    /// </summary> 
    public class IniFile
    ...{
        //文件INI名称 
        public string Path;        /**/////声明读写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);
        //类的构造函数,传递INI文件名 
        public IniFile(string inipath)
        ...{
            // 
            // TODO: Add constructor logic here 
            // 
            Path = inipath;
        }        //写INI文件 
        public void IniWriteValue(string Section, string Key, string Value)
        ...{
            WritePrivateProfileString(Section, Key, Value, this.Path);        }        //读取INI文件指定 
        public string IniReadValue(string Section, string Key)
        ...{
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.Path);
            return temp.ToString();        }
        /**//// <summary>
        /// 验证文件是否存在
        /// </summary>
        /// <returns>布尔值</returns>
        public bool ExistINIFile()
        ...{
            return File.Exists(this.Path);
        }    } 
}
想請問下,那怎麼才能取出INI的內容呢?看上面應該是用IniReadValue()這個方法,但牠裡面的參數是填上什麼呢?請指教指教。

解决方案 »

  1.   


    [aaaa]
    bb=1IniReadValue()里
    Section=aaaa
    key=bb
    获得的是1
      

  2.   

    不太明白說的什麼,那KEY是自己定的?那又怎麼讀到INI中的內容呢?
      

  3.   

    我的INI內容是這樣的
    [SAM]
    Connection=uid=sa;pwd=123;driver={SQL Server};server=.;database=SAMCAT;timeout=1000000
    DateFormat=MDY
    SourceFrom=uid=sa;pwd=123;driver={SQL Server};database=SAMCAT;server=.;timeout=1000000
    FilePath=C:\VBA\lists
    BackupFilePath=C:\VBA\lists\Backup
    UnzipPath=C:\34343我試了把 [SAM],connection作為參數,但不行啊,會報錯,大概說什麼 DLL kernel32找不到指定的模組。
    到底要怎麼讀啊?
      

  4.   

    INI文件内容:
    [AA]
    BB=123
    类:
    class IniFile
        {
            [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 string FilePath = "";//ini文件路径
            //获取文件路径
            public IniFile(string IniFilePath)
            {
                FilePath = IniFilePath;
            }
            //写文件
            public void WriteIniFile(string WantSetSection, string WantSetKey, string WantSetval)
            {
                WritePrivateProfileString(WantSetSection, WantSetKey, WantSetval, this.FilePath);
            }
            //读文件
            public string ReadIniFile(string WantGetSection, string WantGetkey)
            {
                StringBuilder ReadTemp = new StringBuilder(255);
                int i = GetPrivateProfileString(WantGetSection, WantGetkey, "", ReadTemp, 255, this.FilePath);
                return ReadTemp.ToString();
            }
        }
    使用类:
         IniFile IniFIle = new IniFile("\\config.ini");
         global.server = IniFIle.ReadIniFile("AA", "BB");我是这样用的,反正用起来是正常的,你对比看看
      

  5.   

    ini应该可以与txt的内容取法一样吧
      

  6.   

    Read/Write XML files, Config files, INI files, or the Registry
    By Alvaro Mendez
    http://www.codeproject.com/csharp/readwritexmlini.asp
      

  7.   

    讲以下几点吧:
    1.赞同patrickpan的意见,对.NET而言,不像以前一样有什么INI之类的特殊文件,在他眼里都是普通的文本配置文件,他对XML支持比较好,所以一般用XML文件。
    2.你要用.INI文件没问题,首先要搞清楚这个文件其中的概念section,key,value及操作系统对应的API函数的原型,我看你上面的文本量读取函数GetPrivateProfileString,你再次封装时把缓冲区写死了只有255个字节,万一超长了怎么办,我觉得这才是程序设计最重要考虑的东西--健壮性。
    3.尽量少用底层的API函数,如果想用,你就去学C/C++,要不C#也没什么意义....
      

  8.   

    我也遇到了问题,就是用的API函数和大家给的都一样,但是就是读不到ini文件的值,不知道怎么办
      

  9.   

    具体的实例,你看这个吧,写得非常详细。
    http://zhangshun.sysdn.name/csharpaspdotnet/7.html