c# - 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]);
}
当我使用此代码时,我可以看到一个消息框,其中只有我列表中的第一个代理,但我想将所有代理逐行加载到一个字符串中
解决方案
您正在使用File.ReadAllLines
which 返回一个字符串数组,每个字符串都是文件中的一行,然后您只需使用该数组中的第一行TEST[0]
并将其显示在您的消息框中。
您可能希望使用File.ReadAllText
将所有文件内容加载到单个字符串中,然后您可以简单地将其显示在消息框中:
string test = File.ReadAllText(fileName);
MessageBox.Show(test);
您可以在此处获取更多信息。
推荐阅读
- c# - ASP.NET Core 如何让 UserManager 在控制器中工作?
- nestjs - 当 html 页面在保存时重新加载时,Nestjs/Angular Universal 在节点控制台中输出错误
- c# - Visual Studios 中的正则表达式否定搜索查找/替换
- node.js - 使用 Node 创建系统托盘图标
- concurrency - Kamailio 伪变量 $var 在单个消息处理程序结束之前是否可以安全使用?
- javascript - 使用 selenium 和 python 保存/提交网络表单的问题
- windows - 如何摆脱“c_MAMPRO_unins000_exe”
- python - 从给定字符串中删除尾随模式
- bash - 如何在 Docker 容器中获取交互式 bash shell
- azure-cognitive-search - Azure 搜索门户不再允许单击特定索引名称并查看架构等索引详细信息