0

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();
2
  • Are you setting the current directory? If not your relative locations may not be where you think they are. Commented Sep 17, 2012 at 13:44
  • Locations/working directory are fine here. they're do not seem to be a problem Commented Sep 17, 2012 at 16:08

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

You should add UseShellExecute = false there.
thanks. but strangely, I observe no content of the file "monitor_Details.txt" meaning the file is empty though it's created. Is there anything wrong with ">" write operation?
I'am happy to help you stack_pointer is EXTINCT, maybe your arguments, it's possible
the arguments seem to be correct Aghilas, because they work as such in CmdPrompt. Just that they do not work in C# here :(
0

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 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.