Home > front end >  Why does the process return null if we use the Windows temporary path for Process.Start of a file in
Why does the process return null if we use the Windows temporary path for Process.Start of a file in

Time:10-19

I'm using C# WPF in Windows 10 Enterprise in Administrator user

I have a program that receives the file stored in binary form in SQL Server and puts it in a folder in the TEMP path. For example: there is a photo in the database, I select it and WriteAllBytes in the temp, then using Process.Start(C:\Users\ADMINI~1\AppData\Local\Temp.myiamge.png) I run that photo through the Windows Photo Viewer.

so for control the process has started to know when that would be ended I use this : MyProcess.WaitForExit(); but I get this error because MyProcess is null but it stated process successfully !

enter image description here

CS :

  string TempyPath = @"C:\Users\ADMINI~1\AppData\Local\Temp\tmpDCD8.2e0ea3b3-5b93-40f0-a227-40c85fd0a06b.png"; // return null Process
            string NormalPath = @"C:\tmpFB0A.5e2525f3-d4ef-4eb8-a920-c5c5b5d8402f.png";

//Test1
            var MyProcess0 = Process.Start(new ProcessStartInfo()
            {
                FileName = TempyPath,
                WindowStyle = ProcessWindowStyle.Maximized,
                UseShellExecute = true
            });
//Test2
            var MyProcess = Process.Start(TempyPath);
            MyProcess.WaitForExit();

            if (MyProcess.HasExited)
            {
                MessageBox.Show("You Cloes the Microsoft.Photos");
            }

Error for MyProcess.WaitForExit() :

System.NullReferenceException: 'Object reference not set to an instance of an object.'

MyProcess was null.

enter image description here

What should I do ?

Please guide me

Edit: I suspect three things:

  1. File name
  2. File path
  3. Microsoft.Photos.exe Windows itself

What I need : How to open a file and after opening, wait until the window related to that opened file is closed (Control the status of the opened file in Windows)

CodePudding user response:

Process.Start returns null because when there is no new process started. An existing process is used to open the document instead of creating a new one.

If you really want to start a new process and wait for it to exit, then you should be explicit about which process to actually start:

var path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
var process = Process.Start(new ProcessStartInfo()
{
    FileName = "rundll32.exe",
    Arguments = string.Format(
        "\"{0}{1}\", ImageView_Fullscreen {2}",
        Environment.Is64BitOperatingSystem ?
            path.Replace(" (x86)", "") :
            path
            ,
        @"\Windows Photo Viewer\PhotoViewer.dll",
        TempyPath),
    WindowStyle = ProcessWindowStyle.Maximized,
    UseShellExecute = false
});

process.WaitForExit();

if (process.HasExited)
{
    MessageBox.Show("You Cloes the Microsoft.Photos");
}
  • Related