【发布时间】:2017-01-09 17:36:36
【问题描述】:
我尝试逐行读取Text文件:
static void Main(string[] args)
{
int counter = 0;
string line;
string links = @"D:\links.txt";
// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader(links);
while ((line = file.ReadLine()) != null)
{
Console.WriteLine(line);
counter++;
}
file.Close();
// Suspend the screen.
Console.ReadLine();
}
得到一个错误:
发生了“System.NotSupportedException”类型的未处理异常 在 mscorlib.dll 中
附加信息:不支持给定路径的格式。
我正在使用Windows 10
任何可能导致此错误的建议? (文件存在于该路径中)
【问题讨论】:
-
在声明“链接”时“静态”修饰符是否有效?
-
可能是您不小心在其中有一个零长度字符。检查这个答案:stackoverflow.com/a/24856870/5572757
-
@Tophandour 很好,你可以复制和粘贴他的文字,
@"D:\links.txt".Length显示的确实有一个隐藏字符,删除到@"".Length得到1应该为零时。查找如何再次转换为位以查看字符实际上是什么.. -
这个我没看懂,能详细点吗?
-
旁注:
Console.Write(File.ReadAllText(links));而不是循环和阅读器是一种更短的实现方式。