有没有办法读取2行多行文本框的每一行?在textBox1我有一个包含压缩文件列表的多行字符串使用以下代码:
DirectoryInfo getExpandDLL = new DirectoryInfo(showExpandPath);
FileInfo[] expandDLL = getExpandDLL.GetFiles("*.dl_");
foreach (FileInfo listExpandDLL in expandDLL)
{
textBox1.AppendText(listExpandDLL + Environment.NewLine);
}
Run Code Online (Sandbox Code Playgroud)
目前我的部分代码是这样的:
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();
Run Code Online (Sandbox Code Playgroud)
上面的代码采用textBox1中压缩文件的名称,并在textBox2中重命名,然后运行expand.exe以展开压缩文件.代码基本上以expand.exe为例给出以下命令:
c:\users\nigel\desktop\file.dl_ c:\users\nigel\desktop\file.dll
Run Code Online (Sandbox Code Playgroud)
如果文件夹在textBox1中只包含一行文本,它的效果很好.使用多行文本命令基本上是:
c:\users\nigel\desktop\loadsoffiles.dl_ etc and doesnt …Run Code Online (Sandbox Code Playgroud) 用户可以单击ListBox中的项目,如下所示:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox2.Clear();
listBox2.Items.Clear();
string[] p =
Directory.GetFiles(
textBoxDir.Text,
listBox1.SelectedItem.ToString(),
SearchOption.AllDirectories);
foreach (string open in p)
......
}
Run Code Online (Sandbox Code Playgroud)
一切都很好.但是,如果用户单击ListBox中的空白区域,则会显示以下错误:
System.NullReferenceException
Run Code Online (Sandbox Code Playgroud)
这是因为这行代码:
string[] p =
Directory.GetFiles(
textBoxDir.Text,
listBox1.SelectedItem.ToString(),
SearchOption.AllDirectories);
Run Code Online (Sandbox Code Playgroud)
有没有人有聪明的工作?或者建议我的代码替代?