Using C#, how to Open and Close a jpg or png file using Process?

-1

I'm working on a c# program that needs to open png/jpg file and close the same file after some secs.

when I simply used the ffg:

 public async void display()
        {                
 var process = new Process();
        process.StartInfo.FileName = _filePath;
        process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
        process.StartInfo.UseShellExecute = true;
        process.Start();
        await Task.Delay(1000);
        process.Kill();
        process.Close(); }

But the above throws an exception:

System.InvalidOperationException
  HResult=0x80131509
  Message=No process is associated with this object.
  Source=System
  StackTrace:
   at System.Diagnostics.Process.EnsureState(State state)
   at System.Diagnostics.Process.EnsureState(State state)
   at System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited)
   at System.Diagnostics.Process.Kill()
   at MultiDisplay.Form1.<display>d__2.MoveNext() in ...

So it seems the process created was not actually a process hence the invalid exception.

Although, I can open and close a notepad text file the same way with no issues. I am thinking the .jpg or .png file extension would need some other coding?

Any ideas why what else I'm doing wrong?

c#
asked on Stack Overflow Jan 15, 2021 by user3273573 • edited Jan 17, 2021 by user3273573

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0