我有一个名为Names.txt的文件管理器 以及以下数据
David One And Two/Three
Alex One Two Four And Five/Six
Amanda Two Seven/Ten
Micheal Seven/Nine
这是我的代码:
string[] Name = File.ReadAllLines("Names.txt", Encoding.Default);
我希望将每一行作为字符串返回
"David One And Two/Three" as one string
"Alex One Two Four And Five/Six" as one string
"Amanda Two Seven/Ten" as one string
"Micheal Seven/Nine" as one string ..
我的意思是当我跑步时
for(int i = 0; i < Name.Length, i++)
{
Consol.WriteLine(Name[i]);
}
,输出应为
David One And Two/Three
Alex One Two Four And Five/Six
Amanda Two Seven/Ten
Micheal Seven/Nine
但我得到的是
David One
Alex
Amanda Two
Micheal
和Consol.WriteLine(Name.Length)应为4但我得到6我不知道为什么。即使文件为空,我也会得到Name.Length 6
我的意思是,如果它是这样的
string[] Name = {"David One And Two/Three", "Alex One Two Four And Five/Six", "Amanda Two Seven/Ten", "Micheal Seven/Nine"};
请帮助我,我做错了什么?我在Console和windowsForm中尝试过但同样的问题
答案 0 :(得分:0)
复制/粘贴您的代码,修复拼写错误并为File.ReadAllLines提供完整的文件路径。工作正常。
static void Main(string[] args)
{
string[] Name = File.ReadAllLines("M:\\StackOverflowQuestionsAndAnswers\\40472088\\data.txt", Encoding.Default);
for (int i = 0; i < Name.Length; i++)
{
Console.WriteLine(Name[i]);
}
Console.ReadLine();
}
控制台输出
David One And Two/Three
Alex One Two Four And Five/Six
Amanda Two Seven/Ten
Micheal Seven/Nine
要完成我对您的ForLoop的评论,请按以下方式输入您的问题说明:
for(int i = 0; i < Name.Length, i++)
{
Consol.WriteLine(Name[i]);
}
应该更像这样:
for (int i = 0; i < Name.Length; i++)
{
Console.WriteLine(Name[i]);
}
帮助我们帮助您...复制/粘贴您的代码。