I've got a data collection routine that takes about 10 seconds to run, then it saves data to a CSV file:
string file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Book1.csv");
StreamWriter streamWriter1 = new StreamWriter(File.Open(file, FileMode.Create, FileAccess.Write));
DataTable table = GetMyData(); // takes about 10 seconds
foreach (DataRow row in table.Rows) {
object[] item = row.ItemArray;
for (int i = 0; i < item.Length; i++) {
streamWriter1.Write(item.ToString() + ",");
}
streamWriter1.WriteLine();
}
streamWriter1.Close();
Process.Start("Excel", "book1.csv");
Excel takes a few moments to start as well (5 to 10).
I'd like to modify this technique so that I call Excel just before my data collection so the application will be running by the time I have collected the data, then just have it display the file with the data.
With this in mind, here's what I modified the code to, but it always tells me the file is not there (even though it is):
Process excel = Process.Start("Excel");
if (excel != null) {
string file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Book1.csv");
StreamWriter streamWriter1 = new StreamWriter(File.Open(file, FileMode.Create, FileAccess.Write));
DataTable table = GetMyData(); // takes about 10 seconds
foreach (DataRow row in table.Rows) {
object[] item = row.ItemArray;
for (int i = 0; i < item.Length; i++) {
streamWriter1.Write(item.ToString() + ",");
}
streamWriter1.WriteLine();
}
streamWriter1.Close();
for (int i = 0; i < 100; i++) { // simulate the data collection routine
Thread.Sleep(100);
}
excel.StartInfo.Arguments = file;
excel.StartInfo.ErrorDialog = true;
excel.StartInfo.UseShellExecute = false;
excel.StartInfo.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
try {
excel.Start();
} catch (Exception err) {
Console.WriteLine(err.Message); // <= Message is "The system cannot find the file specified"
}
}
Any thoughts? How would I correctly send the file to the active process?