Reputation: 28
I have a windows form application where on a button click, a program is run that creates a file. I want to output the contents of that (txt) file into a textbox. I can see when debugging the file is created, but I get a File not found error. If I click continue, it then works.
private void button2_Click(object sender, EventArgs e)
{
string fp = EscapeArguments(filepath);
string strCmdText = "some command";
Process.Start("CMD.exe", strCmdText);
string dir = Path.GetDirectoryName(fp);
string name = Path.GetFileNameWithoutExtension(fp);
string txtfp = dir + "\\" + name + + ".txt";
string txtout;
if (File.Exists(txtfp))
{
txtout= File.ReadAllText(txtfp);
textBox1.Text = txtout;
}
}
Upvotes: 0
Views: 81
Reputation: 19496
It sounds like the process hasn't finished creating the file by the time your application is looking for it. Try waiting for the process to exit before continuing:
Process proc = Process.Start("CMD.exe", strCmdText);
proc.WaitForExit();
Upvotes: 5