Click here to Skip to main content
15,887,923 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I have a problem with calling an Exe file in my Console Application
Here is my code part

try
            {
                System.Diagnostics.Process process1 = new System.Diagnostics.Process();
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
                if (System.Environment.OSVersion.Version.Major >= 6) // Windows Vista or higher
                {
                    startInfo.Verb = "runas";
                }
                else { /*Do nothing*/ }

                startInfo.FileName = @"D:\glstats\glstats.exe";
                startInfo.UseShellExecute = false;
                process1.StartInfo = startInfo;
                process1.Start();
                process1.WaitForExit(); 
            }
            catch (Win32Exception ex)
            {
                log.WriteLine("Exec Batch Mode Failed. ");
                log.WriteLine(ex.ToString());

                log.WriteLine("Program Quit: " + DateTime.Now.ToString());
                log.Close();

                return;
            }


The problem is, the process "glstats.exe" appears and close right away in seconds.

Is there any way to wait until glstats.exe finishes the UI?

Software "glstats.exe" needs Administrative right in Windows 10.

I'm stuck with this 2 days already, today I try to ask to see any help since I've done all guideline from Google but it did not work out.

I would like to appreciate your comments
Thank you.

What I have tried:

https://stackoverflow.com/questions/2532769/how-to-start-a-process-as-administrator-mode-in-c-sharp

here is 1st guideline, I read, https://stackoverflow.com/questions/13807429/running-cmd-commands-with-administrator-rights
Posted
Updated 17-Aug-17 20:46pm
Comments
Graeme_Grant 17-Aug-17 21:56pm    
Have you checked process1.ExitCode != 0 to see if there is a problem? 0 = success.

Have you manually tried to run "glstats.exe" as configured above to check that there are no problems with it?
Thomas Le 0903 17-Aug-17 22:17pm    
Hi Graeme_Grant,
Thank you for your support.

I checked process1.ExitCode return "-1".

SO far, I can manually run "glstats.exe" without any problem under Command-line.
That's why I try to automate it by using Console Application.
I want to run it on specific time like each 4 hours for example.
Graeme_Grant 17-Aug-17 22:37pm    
-1 is an error state for that app. Check what the meaning is for that app.
PIEBALDconsult 17-Aug-17 22:12pm    
https://www.codeproject.com/Articles/70864/ProcessCommunicator
Thomas Le 0903 17-Aug-17 22:25pm    
Hi PIEBALDconsult,
Thank you for giving me the Article.
I try to understand and try it as another way to resolve my issue.
Thank you.

For an elevated process, you need UseShellExecute as well:
string appPath = @"...";
string appArgs = @"...";
Process proc = new Process();
ProcessStartInfo si = new ProcessStartInfo(appPath, appArgs);
si.WindowStyle = ProcessWindowStyle.Normal;
si.Verb = "runas";             // UAC elevation required.
si.UseShellExecute = true;     // Required for UAC elevation.
proc.StartInfo = si;
proc.Start();
proc.WaitForExit();
The UAC dialog will appear, the user approves the elevation, the app runs. When it exits, control returns to your app.
 
Share this answer
 
v2
Dear all,
Thank you so much for supporting me about this question.
I resolved the problem already.

Actually, the problem is "Argument" Input.
I corrected it and now it's OK.

the Solution is,
I must have this code line
string output = p.StandardOutput.ReadToEnd();

otherwise, the glstats.exe will be killed in second.
 
Share this answer
 
v2
Comments
BillWoodruff 18-Aug-17 3:11am    
please post comments to either a specific solution, or to your original post. the idea is to keep solutions being solutions.
Thomas Le 0903 18-Aug-17 3:45am    
Hi BillWoodruff,
I modify my comment as the way I fix the problem.
Hopefully, it's useful for other members.
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900