Open A File Programmatically

Open A File Programmatically Banner Image


When testing an app or writing programs that generate files, you may want automate opening the file in code than manually opening those files. The normal process may go like this. Run the program then once the program is complete and then open windows explorer and navigate to the where the file is located.

Process.Start Example 1

The following code checks if the file exists on the hard drive before trying to open the file programmatically.

string textFilePath = @"C:\Temp\testFile.xlsx"; //file location
FileInfo excelFileInfo = new FileInfo(textFilePath);
if (excelFileInfo.Exists)

This will start the process that is associated with the file. In this case, excel will open the xlsx file and that way you won't have open have to double click to open it.

Example 2

This next example still uses the Process class but it is other to configure the process class. See example below.

string textFilePath = @"C:\Temp\testFile.xlsx"; //
Process process = new Process();
process.StartInfo.FileName = textFilePath;
process.StartInfo.UseShellExecute = true;
Get Latest Updates
Comments Section