0

我有一个名为 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 中尝试过,但同样的问题

4

1 回答 1

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]);
}

帮助我们帮助您...复制/粘贴您的代码。

于 2016-11-07T18:57:15.800 回答