我正在制作一个函数,它将从StreamReader中获取行数,但不包括注释(以'//'开头的行)和换行符。
这是我的代码:
感谢您的帮助!
这是我的代码:
private int GetPatchCount(StreamReader reader)
{
int count = 0;
while (reader.Peek() >= 0)
{
string line = reader.ReadLine();
if (!String.IsNullOrEmpty(line))
{
if ((line.Length > 1) && (!line.StartsWith("//")))
{
count++;
}
}
}
return count;
}
我的StreamReader的数据是:
// Test comment
但是我遇到了一个错误,提示“对象引用未设置为对象实例”。有没有办法修复这个错误?
编辑 后来发现问题出在我的StreamReader为空。所以根据musefan和Mr. Smith建议的代码,我想到了以下解决方案:
private int GetPatchCount(StreamReader reader, int CurrentVersion)
{
int count = 0;
if (reader != null)
{
string line;
while ((line = reader.ReadLine()) != null)
if (!String.IsNullOrEmpty(line) && !line.StartsWith("//"))
count++;
}
return count;
}
感谢您的帮助!
"\\"
...算了 ;) - musefan