在C# windows application forms 里面,一个inifile有很多歌section,如何读取ini文件里面,一个section中的key的个数?例如:一个ini文件里面有4个section,第一个有2个key,第二个有4个key,第三个有5个key,第四个有10个key。另外,如何从一个文本文档里读取每行的数据,假如一个文本文档里有21行,每行都有不同的数据(数字或者字符),我第一次想读前2行的数据,第二次要读第3行到滴6行的数据,第三次要读第7行到第11行的数据,第四次要读第12行到第21行的数据?

解决方案 »

  1.   

    方括号的数量就是section的数量
    等号的数量就是key的数量所以,在[]后和下一个[]前,数一下等号的数量就可以了
    知道数量了,读起来就方便多了
      

  2.   

    这里有完整的例子
    http://apps.hi.baidu.com/share/detail/5128972在C#中用xml文件方便些,ini一般在C|C++中操作方便些
      

  3.   


    代码懒得写
    这么简单的问题以后不要再问了...        [DllImport("kernel32")]
            private static extern uint GetPrivateProfileString(
                string lpAppName, // points to section name
                string lpKeyName, // points to key name
                string lpDefault, // points to default string
                byte[] lpReturnedString, // points to destination buffer
                uint nSize, // size of destination buffer
                string lpFileName  // points to initialization filename
            );        /// <summary>
            /// 读取section
            /// </summary>
            /// <param name="Strings"></param>
            /// <returns></returns>
            public  List<string> ReadSections(string iniFilename)
            {
                List<string> result = new List<string>();
                byte[] buf = new byte[65536];
                uint len = GetPrivateProfileString(null, null, null, buf,(uint)buf.Length, iniFilename);
                int k = 0;
                for (int i = 0; i < len; i++)
                    if (buf[i] == 0)
                    {
                        result.Add(Encoding.Default.GetString(buf, k, i - k));
                        k = i + 1;
                    }
                return result;
            }        /// <summary>
            ///  010525 by skep99
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, EventArgs e)
            {
                ReadSections(@"C:\a.ini");
            }
      

  4.   


    睡醒一觉,下面是第一问的答案
            [DllImport("kernel32")]
            private static extern uint GetPrivateProfileString(
                string lpAppName, // points to section name
                string lpKeyName, // points to key name
                string lpDefault, // points to default string
                byte[] lpReturnedString, // points to destination buffer
                uint nSize, // size of destination buffer
                string lpFileName  // points to initialization filename
            );        /// <summary>
            /// 读取section
            /// </summary>
            /// <param name="Strings"></param>
            /// <returns></returns>
            public List<string> ReadSections(string iniFilename)
            {
                List<string> result = new List<string>();
                byte[] buf = new byte[65536];
                uint len = GetPrivateProfileString(null, null, null, buf, (uint)buf.Length, iniFilename);
                int k = 0;
                for (int i = 0; i < len; i++)
                    if (buf[i] == 0)
                    {
                        result.Add(Encoding.Default.GetString(buf, k, i - k));
                        k = i + 1;
                    }
                return result;
            }        /// <summary>
            /// 读取指定区域Keys列表。
            /// </summary>
            /// <param name="Section"></param>
            /// <param name="Strings"></param>
            /// <returns></returns>
            public List<string> ReadSingleSection(string Section, string iniFilename)
            {
                List<string> result = new List<string>();
                byte[] buf = new byte[65536];
                uint lenf = GetPrivateProfileString(Section, null, null, buf, (uint)buf.Length, iniFilename);
                int j = 0;
                for (int i = 0; i < lenf; i++)
                    if (buf[i] == 0)
                    {
                        result.Add(Encoding.Default.GetString(buf, j, i - j));
                        j = i + 1;
                    }
                return result;
            }        /// <summary>
            ///  010525 by skep99
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, EventArgs e)
            {
                string iniFilename = @"C:\a.ini";
                List<string> sectionList = ReadSections(iniFilename);
                foreach (string section in sectionList)
                {
                    List<string> keyList = ReadSingleSection(section, iniFilename);                Console.WriteLine("Section[{0}]中Keys的数量为[{1}]", section, keyList.Count);            }
            }
      

  5.   

    下面是第二问的答案,如有不清楚的再继续问        /// <summary>
            /// 读取文本
            /// </summary>
            public List<string> readTxt(string txtFilename)
            {
                List<string> result = new List<string>();
                try
                {
                    StreamReader sr = new StreamReader(txtFilename);
                    string line = string.Empty;
                    while ((line = sr.ReadLine()) != null)
                    {
                        result.Add(line);
                    }
                    sr.Close();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception: " + e.Message);
                }
                return result;
            }        /// <summary>
            /// 从sourceList里按范围取
            /// </summary>
            /// <param name="sourceList"></param>
            /// <param name="start">设置开始位置,计数从1开始</param>
            /// <param name="end">设置结束位置,计数从1开始</param>
            /// <returns></returns>
            public List<string> getListByRange(List<string> sourceList, int start, int end)
            {
                return sourceList.GetRange(start - 1, end - start + 1);
            }        /// <summary>
            /// 100525 测试按行读取txt by skep99
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, EventArgs e)
            {
                string txtFilename = "C:\\a.ini";
                List<string> lineList = readTxt(txtFilename);
                //获取1-2行数据
                List<string> partList = getListByRange(lineList, 1, 2);
                Console.WriteLine(partList[0]);
                Console.WriteLine(partList[1]);
                //获取3-6行数据
                partList = getListByRange(lineList, 3, 6);
                Console.WriteLine(partList[0]);
                Console.WriteLine(partList[1]);
                Console.WriteLine(partList[2]);
                Console.WriteLine(partList[3]);
                //以下就不写了        }
      

  6.   


    请问第二个问题的答案你这个能做成动态的吗?因为文本文档行数不固定,每次读几行也要根据第一个问题section里面的key的多少去读取
      

  7.   

    我现在遇到的问题是这样:
    用户打开一个test.ini文件(这个ini文件只有section和key,没有value),然后程序自动打开一个test.txt文件(这个文件里面存的是ini文件里面的value,例如第一行是1,第二行是5,等等的数值或者字符。这个文件打开后放在一个变量里面,一共打开了2个文件,这个功能我完成了)然后再UI上动态生成tabpage,label和textbox,我要把ini文件里的section,放在tabpage上(就是一个section创建一个tabpage,这个功能我完成了),把ini文件里面的key放在label里面(这个功能我完成了),把txt文件里面的value放在textbox里面,textbox要对应label(现在就是这个textbox的问题,因为不是所有的value都在一个页面上,因为section的不同,要放在不同的tabpage下)这是我目前的代码:if (File.Exists("..\\..\\temp.dat"))
                        File.Delete("..\\..\\temp.dat");
                    File.Copy(openFileDialog1.FileName, "..\\..\\temp.dat");
                    IniFile file = new IniFile("..\\..\\temp.dat");
                    IniFile file2 = new IniFile(openFileDialog1.FileName + "t");
                    string[] section = file2.GetSectionNames();
                    tabControl1.TabPages.Clear();
                    treeView1.Nodes.Clear();
                    TreeNode node = new TreeNode(Path.GetFileName(openFileDialog1.FileName));
                    treeView1.Nodes.Add(node);
                    treeView1.SelectedNode = node;
                    for (int j = 1; j < section.Count(); j++)
                    {
                        TabPage page1 = new TabPage(section[j]);
                        tabControl1.TabPages.Insert(j - 1, page1);
                        page1.Name = section[j];
                        page1.BackColor = Color.White;
                        page1.AutoScroll = true;
                        treeView1.SelectedNode.Nodes.Add(section[j].ToString());
                        string[] key = file2.GetKeyNames(section[j]);
                        for (int i = 0; i < key.Count(); i++)
                        {
                            Label l = new Label();
                            l.Parent = page1;
                            l.Left = 20;
                            l.AutoSize = true;
                            l.Top = 20 + i * 20;
                            l.Name = i.ToString();
                            l.Text = key[i] + " :";
                         //这里开始是动态生成textbox,我这个代码是错的
                            for (int a = 0; a < 1; a = a + key.Count())
                            {
                                TextBox t = new TextBox();
                                t.Parent = page1;
                                t.Left = 320;
                                t.AutoSize = true;
                                t.Top = 20 + i * 20;
                                //t.Text = 
                                //t.Name =
                                t.Width = 200;
                                t.BorderStyle = BorderStyle.Fixed3D;
                                t.TextChanged += new EventHandler(t_TextChanged);
                            }
                        }
                    }
                    treeView1.ExpandAll();
                    treeView1.SelectedNode = null;
      

  8.   


    根据section去读取keys?是不是这么理解?
      

  9.   

    对~例子:text.ini文件内容
    [aa]
    keyword1
    keyword2
    123
    [bb]
    keyword3
    keyword4
    [cc]
    key
    key2test.txt文件内容
    1
    2
    33
    34
    45
    01
    02显示在ui上面就是要:
    【tabpage】aa,bb,cc 3个tabpage
    【label】keyword1,keyword2,123 显示在【aa】的tabpage下
    【textbox】1,2,33显示在【aa】的tabpage下
    【label】keyword3,keyword4 显示在【bb】的tabpage下
    【textbox】34,45显示在【bb】的tabpage下
    【label】key,key2 显示在【cc】的tabpage下
    【textbox】01,02显示在【cc】的tabpage下
      

  10.   


    试试,是这个方法吧?        /// <summary>
            /// 按section取key的值
            /// </summary>
            /// <param name="Section"></param>
            /// <param name="iniFilename"></param>
            /// <returns></returns>
            public string ReadString(string Section, string key, string iniFilename)
            {
                byte[] buf = new byte[2048];
                uint cnt = GetPrivateProfileString(Section, key, null, buf, (uint)buf.Length, iniFilename);
                return Encoding.Default.GetString(buf, 0, (int)cnt);
            }
      

  11.   

    你那个是在一个文件里面的吧?在一个文件里面读取的话我会:
    if (File.Exists("..\\..\\temp.dat"))
                        File.Delete("..\\..\\temp.dat");
                    File.Copy(openFileDialog1.FileName,"..\\..\\temp.dat");
                    IniFile file = new IniFile("..\\..\\temp.dat");
                    string[] section = file.GetSectionNames();
                    tabControl1.TabPages.Clear();
                    treeView1.Nodes.Clear();
                    TreeNode node = new TreeNode(Path.GetFileName(openFileDialog1.FileName));
                    treeView1.Nodes.Add(node);
                    treeView1.SelectedNode = node;
                    for (int j = 1; j < section.Count(); j++)
                    {
                        TabPage page1 = new TabPage(section[j]);
                        tabControl1.TabPages.Insert(j - 1, page1);
                        page1.Name = section[j];
                        page1.BackColor = Color.White;
                        page1.AutoScroll = true;
                        treeView1.SelectedNode.Nodes.Add(section[j].ToString());
                        string[] key = file.GetKeyNames(section[j]);                    for (int i = 0; i < key.Count(); i++)
                        {
                            Label l = new Label();
                            l.Parent = page1;
                            l.Left = 20;
                            l.AutoSize = true;
                            l.Top = 20 + i * 20;
                            l.Name = i.ToString();
                            l.Text = key[i] + " :";                        string value = file.GetString(section[j], key[i], "");
                            TextBox t = new TextBox();
                            t.Parent = page1;
                            t.Left = 320;
                            t.AutoSize = true;
                            t.Top = 20 + i * 20;
                            t.Name = key[i];
                            t.Text = value;
                            t.Width = 200;
                            t.BorderStyle = BorderStyle.Fixed3D;
                            t.TextChanged += new EventHandler(t_TextChanged);
                        }
                    }
                    treeView1.ExpandAll();
                    treeView1.SelectedNode = null;
    这样子可以实现读取一个ini文件里面的东西,并且动态生成,但是像我说的2个文件的就不行,你的方法也不行