【发布时间】:2018-08-21 14:45:43
【问题描述】:
我在 C# 上使用 OpenFileDialog 组件来选择要处理的多个文件。
问题是当程序完成foreach循环的执行时,它正在重复“private void openFileDialog1_FileOk(object sender, CancelEventArgs e)”方法,弄乱了我的excel电子表格。
如何处理?
这是我的代码:
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
int i = 2;
Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
xla.Visible = true;
Microsoft.Office.Interop.Excel.Workbook wb = xla.Workbooks.Add(Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet);
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)xla.ActiveSheet;
ws.Cells[1, 1] = "Test case";
ws.Cells[1, 2] = "Q Score";
ws.Cells[1, 3] = "TC Score";
foreach (String file in openFileDialog1.FileNames)
{
try
{
StreamReader arq = new StreamReader(file);
string Title = file;
Title = Title.Remove(0, Title.LastIndexOf("\\") + 1);
Title = Title.Remove(Title.LastIndexOf("."), Title.Length - Title.LastIndexOf("."));
string Title_aux = Title;
F.Text = "MSA-GA: " + Title;
string texto = arq.ReadToEnd();
arq.Close();
arq.Dispose();
openFileDialog1.Dispose();
texto = texto.Replace(" ","");
texto = texto.Replace("\r\n\r\n", "\r\n");
texto = texto.Replace("\r\n\r\n", "");
StreamWriter gravar = new StreamWriter(@"cases\" + Title + ".FASTA");
gravar.Write(texto);
gravar.Close();
gravar.Dispose();
string Title_ref = Title.Substring(0, (Title.Length - 2));
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c program -test cases\\" + Title + ".FASTA " + "-ref cases\\" + Title_ref;
//p.StartInfo.Arguments = "/C dir";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
int pos_ini = output.IndexOf("Q=")+2;
string q_score = output.Substring(pos_ini, (output.IndexOf(';', pos_ini) - pos_ini));
pos_ini = output.IndexOf("TC=") + 3;
string tc_score = output.Substring(pos_ini, (output.IndexOf("\r\n", pos_ini) - pos_ini));
q_score=q_score.Replace('.', ',');
tc_score=tc_score.Replace('.', ',');
ws.Cells[i, 1] = Title;
ws.Cells[i, 2] = q_score;
ws.Cells[i, 3] = tc_score;
i++;
}
catch
{
}
}
}
方法如下:
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
正在通过openFileDialog1.ShowDialog()调用
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "Open Fasta File";
openFileDialog1.InitialDirectory = Application.StartupPath;
openFileDialog1.ShowDialog();
}
【问题讨论】:
-
永远不要使用空的 try-catch。
-
是的,我知道。这只是一个快速测试。但这不是问题:(谢谢
-
如果
openFileDialog1_FileOk()被多次调用,那么您应该提供代码来显示它是如何被调用的。我没有在方法本身中看到问题。 -
它在 openFileDialog1.ShowDialog() 执行时被调用。我已经编辑了帖子。
-
不要使用 FileOk 事件。它旨在拒绝选定的文件,而不是使用对话结果。在 ShowDialog() 返回 DialogResult.OK 之前不要启动 foreach 循环
标签: c# openfiledialog