| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1260 人关注过本帖
标题:C#循环问题(有读取ini文件经验的帮帮忙)在线等 急
只看楼主 加入收藏
cfchhh
Rank: 1
等 级:新手上路
帖 子:85
专家分:0
注 册:2005-5-8
收藏
 问题点数:0 回复次数:1 
C#循环问题(有读取ini文件经验的帮帮忙)在线等 急
StreamReader sr = new StreamReader(@"C:\Documents and Settings\SHCNC001\My Documents\Visual Studio Projects\ShopGiludSystem\ShopGiludSystem\1.ini");//读取ini文件
   string ReaderLine;
   ArrayList al = new ArrayList();
   do
   {
    ReaderLine = sr.ReadLine();//每次读取一行
    if(ReaderLine=="[Section]") continue;
    al.Add(ReaderLine);//读取后放入一个数组
   }while(ReaderLine!=""&&ReaderLine!=null);
现在有一个问题就是 用do while 循环会再ini文件里多读一行(行的内容是null),请问那位同行高手,怎么能不让这个null加到数组中去,我试过再读取数组后用if(ReaderLine!=""&&ReaderLine!=null) continue;想跳过,避免读null到数组,可是这样系统会报错,请问你们知道有什么方法解决这问题吗,或者用其他循环(但是ini文件里的数据行又是不确定的,用for就不行了,用while呢也好像不大好办)
搜索更多相关主题的帖子: ini 文件 经验 在线 
2005-10-13 14:21
wanderingies
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-8-6
收藏
得分:0 
Hashtable aPlugins = new Hashtable();
StreamReader sr = new StreamReader(sPath);
int IndexOf;
string ReaderLine;
do
{
    ReaderLine = sr.ReadLine();
    if (ReaderLine == null) break;
    if (ReaderLine.Substring(0, 1) == "[" && ReaderLine.Substring(ReaderLine.Length, 1) == "]") continue;

    IndexOf = ReaderLine.IndexOf('=');
    string aa = ReaderLine.Substring(0, IndexOf);
    aPlugins.Add(ReaderLine.Substring(0, IndexOf), ReaderLine.Substring(ReaderLine.Length - 1, 1));
} while (true);

我在你的基础上改的
if (ReaderLine == null) break; 这一句必须要,我在vs2012下验证通过

首先我认为既然是键对值,那么用哈希表来存再适合不过了
Hashtable aPlugins = new Hashtable();

再者既然是循环读那么中括号内的值就不确定,我是这样写的,当然还需要去掉可能存在的空格
if (ReaderLine.Substring(0, 1) == "[" && ReaderLine.Substring(ReaderLine.Length, 1) == "]") continue;

ini 文件内容
[Shortcuts]
Mouse=0
Keyboard=1
Waitcommand=0
WaitHelper=H
WaitSetting=S
2013-08-06 21:45
快速回复:C#循环问题(有读取ini文件经验的帮帮忙)在线等 急
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011475 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved