I'm writing a program that launches a random file in s directory. the file can be of any type, but mostly video or image files. Each time I launch a file I want to close the previous opened one.
Code :
string FolderSelected = "";
string FileName = "";
Process proc;
List<string> FilesDisplayed = new List<string>();
private void button2_Click(object sender, EventArgs e)
{
if (FolderSelected == string.Empty)
FolderSelected = Properties.Settings.Default.FilesDefaultFolder;
if (proc != null)
{
proc.CloseMainWindow();
proc.Close();
}
FileName = FetchRandomFile();
proc = Process.Start(FileName);
}
Problem is, that I keep getting proc = null
(the file is launched properly) and I cannot fetch the previously opened process in order to close it. I know that .NET reuses processes and that's why it returns Null but I need to override this behavior.
static Process Start(ProcessStartInfo startInfo)
can returnnull
, if the process didn't start (or is already started). – Law