I've the below code where I use DevCon.exe to capture something and write it in a file. I parse this file for a need.
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/C devcon.exe find = port *monitor* > monitor_Details.txt";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = true;
p.StartInfo.Verb = "runas";
p.Start();
Unfortunately, with this code, I dont see any textfile created. So, here shell commands are not considered though I mentioned.
Same command is working in CMDLine.
Can anyone pls help as to what's going wrong?
I tried with below code as well and it does not work.
Process p = new Process();
p.StartInfo.FileName = "devcon.exe";
p.StartInfo.Arguments = "find = port *monitor* > monitor_Details.txt";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = true;
p.StartInfo.Verb = "runas";
p.Start();
You can add theses lines - based on RedirectStandardOutput
p.StartInfo.RedirectStandardOutput = true;
.....
p.Start();
string result = p.StandardOutput.ReadToEnd();
Link : http://msdn.microsoft.com/fr-fr/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx
I had the exact same problem. A workaround is to provide the UserName and Password parameters for the process as well as the "runas" verb. This will make the new process start elevated and be able to Read/Write files. I don't have clear explanation but it worked for me.
p.StartInfo.Verb = "runas";
p.StartInfo.UserName = Environment.UserName;
p.StartInfo.Password = PromptUserPassword(); //Get password as SecureString
Related
I have a console program written in c++, and I would like to continually check its output to use in my c# project.
This is the code I've tried to steal from another StackOverflow question, but with no luck.
Edit:( By no luck, I mean: the output is pretty much empty, although if I set p.StartInfo.CreateNoWindow to false, I can see my script.exe launch and output into its window. )
Process p = new Process();
p.StartInfo.FileName = #"external\script.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine("Output:");
Console.WriteLine(output);
I had a similar issue and it had to do with where the output was going. The solution was to run cmd and execute my script from the cmd process. See this answer to another question for more information. reading from stdout and stderr together Your new code would look like:
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c yourscriptpath.exe 2>&1";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine("Output:");
Console.WriteLine(output);
As part of my program I need to run a batch file.
The person I'm doing it for doesn't want anyone to be able to view the batch script, so I tried to include it in a WinForms solution, but I can't get it to run, I just get a time out error.
Michael,
You can do it two different way.
To provide script file in Process, but it will expose your code, as its open in file.
You can start command prompt by your process and supply your code as an argument.
var p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c mycmd.exe 2>&1";
OR
var p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = #"/c dir \windows";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = false;
p.OutputDataReceived += (a, b) => Console.WriteLine(b.Data);
p.ErrorDataReceived += (a, b) => Console.WriteLine(b.Data);
p.Start();
p.BeginErrorReadLine();
p.BeginOutputReadLine();
p.WaitForExit();
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 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.
I am trying to create process on a remote machine using using System.Diagnostics.Process class.
I am able to create a process. But the problem is, creating a service is take a long time and console window is displayed.
Another annoying thing is the console window is displayed on top of my windows form and i cant do any other operations on that form.
I have set all properties like CreateNoWindow = true,
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
but still it shows the console window. even i have redirected output and errors to seperate stream but no luck.
Is there any other way to hide the Console window? Please help me out .
Here is the part of my code i used to execute sc command.
Process proc = new Process();
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.FileName = "sc";
proc.StartInfo.Arguments = string.Format(#"\\SYS25 create MySvc binPath= C:\mysvc.exe");
proc.StartInfo.RedirectStandardError = false;
proc.StartInfo.RedirectStandardOutput = false;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.WaitForExit();
I had a similar issue when attempting to start a process without showing the console window. I tested with several different combinations of property values until I found one that exhibited the behavior I wanted.
Here is a page detailing why the UseShellExecute property must be set to false.
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.createnowindow.aspx
Under Remarks section on page:
If the UseShellExecute property is true or the UserName and
Password properties are not null, the CreateNoWindow property
value is ignored and a new window is created.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = fullPath;
startInfo.Arguments = args;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
Process processTemp = new Process();
processTemp.StartInfo = startInfo;
processTemp.EnableRaisingEvents = true;
try
{
processTemp.Start();
}
catch (Exception e)
{
throw;
}
I've had bad luck with this answer, with the process (Wix light.exe) essentially going out to lunch and not coming home in time for dinner. However, the following worked well for me:
Process p = new Process();
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
// etc, then start process
This should work, try;
Add a System Reference.
using System.Diagnostics;
Then use this code to run your command in a hiden CMD Window.
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
cmd.StartInfo.Arguments = "Enter your command here";
cmd.Start();
This doesn't show the window:
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
cmd.StartInfo.CreateNoWindow = true;
...
cmd.Start();