The following code is running fine on my computer but it hangs on office computer
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = #"c:\temp\" + filename;
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = new Process();
p.StartInfo = info;
p.Start();
p.WaitForInputIdle();
System.Threading.Thread.Sleep(3000);
if (false == p.CloseMainWindow())
p.Kill();
Please help
Some computers are slower then others so the process is exiting before it is finished. Increasing the thread sleep time fixed the problem.
Related
I want to press button and it should continue and display percent time when i click another page, code should on background. After that i click this page it display me results belong running .exe process.
How can i do that? I need your help.
ProcessStartInfo startInfo = new ProcessStartInfo();
Process p = new Process();
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.Arguments = "/Scan http://localhost/deneme/sunucu.html /Profile web_applications /SaveToDatabase –ScanningMode=Heuristic –UseAcuSensor=TRUE –EnablePortScanning=TRUE";
startInfo.FileName = #"C:\\Program Files (x86)\\Acunetix\\Web Vulnerability Scanner 9.5\wvs_console.exe";
p.StartInfo = startInfo;
p.OutputDataReceived += (sender, args) => Console.WriteLine("received output: {0}", args.Data);
p.Start();
p.BeginOutputReadLine();
StreamReader output = p.StandardOutput //i tried beginoutputreadline
p.WaitForExit();
ListBox1.Items.Add(output.tostring());
Regards,
I have developed one windows application in which i have some implemented feature now i want to implement optimize hard disk so i got to know about defrag.exe .so i wrote some code but its not working for me. can anyone what am doing wrong ?
Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
p.StartInfo.Verb = "runas";
p.StartInfo.FileName =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "Defrag.exe");
p.StartInfo.Arguments = #"c:\ /A";
try
{
p.Start();
p.WaitForExit();
string a= p.StandardOutput.ToString();
See my comment on your previous post. That aside, you need to set a few extra parameters - working sample below. You may also need to elevate privileges in order for your scenario to work. If this is the case, then see this post.
static void Main(string[] args)
{
Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
p.StartInfo.Verb = "runas";
p.StartInfo.FileName =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "Defrag.exe");
p.StartInfo.Arguments = #"c:\ /A";
// Additional properties set
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
// Fixed your request for standard with ReadToEnd
string result = p.StandardOutput.ReadToEnd();
p.WaitForExit();
}
Adding another option - try the below. To use runas, you need to set StartInfo.UseShellExecute = true which means you cant redirect the standard output - would this still work for you? Another option is to run your whole program as admin How do I force my .NET application to run as administrator? - this will allow you to redirect your output and run with elevated permissions.
static void Main(string[] args)
{
Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
p.StartInfo.Verb = "runas";
p.StartInfo.FileName =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "Defrag.exe");
p.StartInfo.Arguments = #"c:\ /A";
// Additional properties set
//p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = true;
//p.StartInfo.CreateNoWindow = true;
p.Start();
// Fixed your request for standard with ReadToEnd
//string result = p.StandardOutput.ReadToEnd();
p.WaitForExit();
}
I currently have a portion of code that creates a new Process and executes it from the shell.
Process p = new Process();
...
p.Start();
p.WaitForExit();
This keeps the window open while the process is running, which is great. However, I also want to keep the window open after it finishes to view potential messages. Is there a way to do this?
It is easier to just capture the output from both the StandardOutput and the StandardError, store each output in a StringBuilder and use that result when the process is finished.
var sb = new StringBuilder();
Process p = new Process();
// redirect the output
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
// hookup the eventhandlers to capture the data that is received
p.OutputDataReceived += (sender, args) => sb.AppendLine(args.Data);
p.ErrorDataReceived += (sender, args) => sb.AppendLine(args.Data);
// direct start
p.StartInfo.UseShellExecute=false;
p.Start();
// start our event pumps
p.BeginOutputReadLine();
p.BeginErrorReadLine();
// until we are done
p.WaitForExit();
// do whatever you need with the content of sb.ToString();
You can add extra formatting in the sb.AppendLine statement to distinguish between standard and error output, like so: sb.AppendLine("ERR: {0}", args.Data);
This will open the shell, start your executable and keep the shell window open when the process ends
Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "CMD.EXE";
psi.Arguments = "/K yourmainprocess.exe";
p.StartInfo = psi;
p.Start();
p.WaitForExit();
or simply
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "CMD.EXE";
psi.Arguments = "/K yourmainprocess.exe";
Process p = Process.Start(psi);
if(p != null && !p.HasExited)
p.WaitForExit();
Be carefull espacially on switch /k, because in many examples is usually used /c.
CMD /K Run Command and then return to the CMD prompt.
CMD /C Run Command and then terminate
var p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/k yourmainprocess.exe";
p.Start();
p.WaitForExit();
Regarding: "Member Process.Start(ProcessStartInfo) cannot be accessed with an instance reference; qualify it with a type name instead"
This fixed the problem for me....
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "CMD.EXE";
psi.Arguments = "/K yourmainprocess.exe";
Process p = Process.Start(psi);
p.WaitForExit();
When I run the package in Visual Studio the files print. When I deploy it and run it as a SQL Server job the files do not print. Any ideas? The package RUN AS is a domain admin account.
Here is the code.
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "PrintTo";
info.Arguments = "\"printername\"";
info.FileName = path;
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = new Process();
p.StartInfo = info;
p.Start();
p.WaitForInputIdle();
System.Threading.Thread.Sleep(3000);
if (false == p.HasExited)
p.Kill();
I have a doubt, when I create a new process iexplore, and this process opens one single page, I would like know if the page loads correctly. How can I learn if the page load fails ?. Is there any way to catch an error from iexplore process?
I have this demo code, but doesn't work correctly
string navegador = "C:\\program files (x86)\\Internet Explorer\\iexplore.exe";
Process p = new Process();
p.StartInfo.FileName = navegador;
ProcessStartInfo processStartInfo = new ProcessStartInfo(navegador);
p.EnableRaisingEvents = false;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo = processStartInfo;
p.StartInfo.Arguments = "google.com";
p.Start();
Process currentProcess = Process.GetCurrentProcess();
Process[] localByName = Process.GetProcessesByName("iexplore");
Process[] localAll = Process.GetProcesses();
p.OutputDataReceived += new DataReceivedEventHandler(
prsProjectTypes_OutputDataReceived);
p.BeginOutputReadLine();
errorMessage = p.StandardError.ReadToEnd();
p.WaitForExit();
Thank you for any help.
Assuming you are working WPF, you should look at the Web Browser control rather than launching a separate process. This will give you access to the LoadCompleted event which seems to be what you're looking for.