首页 > 解决方案 > C# String ReadAllFile 每行

问题描述

我希望当我按下按钮时,它会打开一个OpenFileDialog以加载测试文件的内容,例如 proxys.txt。

我希望将 proxys.txt 中的所有代理解析为可用于 HttpWebRequest 的字符串。

我的代码Form1:

OpenFileDialog openFile1 = new OpenFileDialog();
openFile1.Filter = "Load Proxys File |*.txt";

if (openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    string[] readText = File.ReadAllLines(openFile1.FileName);
    string fileName = openFile1.FileName;
    string[] TEST = File.ReadAllLines(fileName);

    MessageBox.Show(TEST[0]);
}

当我使用此代码时,我可以看到一个消息框,其中只有我列表中的第一个代理,但我想将所有代理逐行加载到一个字符串中

标签: c#

解决方案


您正在使用File.ReadAllLineswhich 返回一个字符串数组,每个字符串都是文件中的一行,然后您只需使用该数组中的第一行TEST[0]并将其显示在您的消息框中。

您可能希望使用File.ReadAllText将所有文件内容加载到单个字符串中,然后您可以简单地将其显示在消息框中:

string test = File.ReadAllText(fileName);
MessageBox.Show(test);

您可以在此处获取更多信息。


推荐阅读