【问题标题】:Read Each Line of 2 Multiline Text Box读取每行 2 个多行文本框
【发布时间】:2013-04-15 20:52:26
【问题描述】:

有没有办法读取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_
  • evenmore.dl_

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

  • somefile.dll
  • someMore.dll
  • evenmore.dll

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

谢谢!

【问题讨论】:

  • 我不完全确定您的问题到底是什么,但如果它只是基于换行符解析多行文本,那么您应该能够很好地执行 Regex.Split
  • 为什么要把DirectoryInfo.GetFiles的所有输出都放到文本框中,然后再尝试解析里面的字符串?
  • @I4V 它的 textBox1 以列表的形式显示压缩文件的名称及其在 dl_ 中的扩展名
  • l4V 的意思是你的文本框应该只用于显示,你不应该解析它。您已经解析了数据,因此请妥善保管并在需要时使用。
  • 对,我现在明白你的意思了。但是expand.exe的工作方式是我需要压缩文件的路径,即.dl_然后是扩展文件的路径和名称:c:\users\nigel\desktop\file.dl_ c:\users\nigel\desktop \文件.dll。因此,为什么我尝试解析 textBox1!但也许我应该解析 Directory.GetFiles 数据...嗯,谢谢。

标签: c#


【解决方案1】:

您需要做的是循环遍历字符串数组,而不是使用单个字符串。请注意,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();
}

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

【讨论】:

  • 谢谢你,我已经玩过了。问题是我的代码目前的工作方式是我重命名了 textBox1 的每一行。因此,第一行在 textBox2 中从 somefile.dl_ 重命名为 somefile.dll。有没有办法重命名“线”?谢谢。
  • 绝对棒极了。非常感谢罗布。我可以看到代码背后的逻辑。但是经验如此之少,我自己并没有想到。再次感谢!
【解决方案2】:

Google 的第一次点击告诉我们以下内容:

string txt = TextBox1.Text;
string[] lst = txt.Split(new Char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);

【讨论】:

  • 您好 Grumble85,感谢您的回复。我不认为拆分会有所帮助,因为文本框 1 已经是一个多行文本,每行一行。
  • 那是垃圾。使用Environment.NewLine
  • @banging 是的,我一直在使用 Envionrment.Newline
【解决方案3】:

你可以试试这段代码

string a = txtMulti.Text;

string[] delimiter = {Environment.NewLine};

string[] b = a.Split(delimiter, StringSplitOptions.None); 

【讨论】:

    猜你喜欢
    • 2015-01-28
    • 2012-10-09
    • 1970-01-01
    • 1970-01-01
    • 2019-08-16
    • 1970-01-01
    • 2016-09-14
    • 2011-08-20
    • 2011-01-15
    相关资源
    最近更新 更多