本质上,我有一个Do..While
循环遍历文本文件中的一些行。我想处理一行,返回一个值(工作或没有),然后移动到下一行。
我有一个ProcessTXT
接受 2 个字符串的函数。 Source
和Destination
新文件。
有没有办法为ReturnedValue string =
结果设置 a 并让后台工作人员检查变量是否更改?如果是这样,将此值添加到列表框中?
private void TranslatePOD(string strSource, string strDest,)
{
TextWriter tw = new StreamWriter(strDest);
TextReader tr = new StreamReader(strSource);
do
{
//My Code doing stuff
//Need to send a result somehow now, but i have more work to do in this loop
//Then using tw.writeline() to write my results to my new file
} while (tr.ReadLine() != null);
}
编辑:当前使用 Yield 的测试代码。我的输出是“TestingGround.Form1+d__0”。我做错什么了吗?
namespace TestingGround
{
public partial class Form1 : Form
{
static IEnumerable<string> TestYield(string strSource)
{
TextReader tr = new StreamReader(strSource);
string strCurLine = System.String.Empty;
while ((strCurLine = tr.ReadLine()) != null)
{
yield return strCurLine;
}
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string MySource = System.String.Empty;
MySource = @"C:\PODTest\Export Script\Export\Shipment List.csv";
listBox1.Items.Add(TestYield(MySource));
}
}