3

有没有办法读取2个多行文本框的每一行?在textBox1我有一个多行字符串,其中包含使用以下代码的压缩文件列表:

DirectoryInfo getExpandDLL = new DirectoryInfo(showExpandPath);
FileInfo[] expandDLL = getExpandDLL.GetFiles("*.dl_");
foreach (FileInfo listExpandDLL in expandDLL)
{
    textBox1.AppendText(listExpandDLL + Environment.NewLine);
}

目前我的部分代码是这样的:

textBox2.Text = textBox1.Text.Replace("dl_", "dll");
string cmdLine = textDir.Text + "\\" + textBox1.Text + " " + textDir.Text + "\\" + textBox2.Text;
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.FileName = "expand.exe";
startInfo.UseShellExecute = true;
startInfo.Arguments = cmdLine.Replace(Environment.NewLine, string.Empty);
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();

上面的代码在 textBox1 中取压缩文件的名称并在 textBox2 中重命名它,然后运行 ​​expand.exe 来展开压缩文件。代码基本上给出了expand.exe下面的命令作为例子:

c:\users\nigel\desktop\file.dl_ c:\users\nigel\desktop\file.dll

如果文件夹在 textBox1 中仅包含一行文本,则效果很好。对于多行文本,命令基本上是:

c:\users\nigel\desktop\loadsoffiles.dl_ etc and doesnt work!

有没有办法读取textBox1的每一行,更改字符串并将其放入textBox2然后将命令传递给expand.exe?

string cmdLine = textDir.Text + "\\" + lineOFtextBox1 + " " + textDir.Text + "\\" + lineOftextBox2;

编辑:为了清楚起见:TextBox1 包含:

  • somefile.dl_
  • someMore.dl_
  • 甚至更多.dl_

作为一条多线。我的代码采用该多行文本并将其放入 textBox2 中,因此它包含:

  • 一些文件.dll
  • someMore.dll
  • 更多.dll

有没有办法读取每一行/获取 textBox1 和 textBox2 的每一行并用它做“东西”?

谢谢你!

4

3 回答 3

10

您需要做的是遍历字符串数组,而不是使用单个字符串。请注意,TextBox 有一个“Lines”属性,可以为您提供已拆分为数组的行

foreach(string line in textBox1.Lines)
{
    //your code, but working with 'line' - one at a time

}

所以我认为你的完整解决方案是:

foreach (string line in textBox1.Lines)
{
    string cmdLine = textDir.Text + "\\" + line + " " + textDir.Text + "\\" + line.Replace("dl_", "dll");
    var process = new Process
        {
            StartInfo = new ProcessStartInfo
                {
                    FileName = "expand.exe",
                    Arguments = cmdLine.Replace(Environment.NewLine, string.Empty),
                    WindowStyle = ProcessWindowStyle.Normal,
                    UseShellExecute = true
                }
        };
    process.Start();
    process.WaitForExit();
}

请注意,我们正在为您的文本框中的每一行启动一个进程,我认为这是正确的行为

于 2013-04-22T20:37:07.587 回答
1

第一次谷歌点击告诉我们以下内容:

string txt = TextBox1.Text;
string[] lst = txt.Split(new Char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
于 2013-04-22T20:12:57.490 回答
1

您可以尝试使用此代码

string a = txtMulti.Text;

string[] delimiter = {Environment.NewLine};

string[] b = a.Split(delimiter, StringSplitOptions.None); 
于 2013-04-22T20:30:41.030 回答