views:

49

answers:

4

In my previous question I detailed the problem I encountered.

One of the solutions is to simulate a user typing the command in the prompt. I don't know how to do it in .NET, since the usual way of doing this is by using System.Diagnostics.Process which causes the error.

I wonder if there is any alternative to that class? What are the differences between using that class and typing the command as a user?

+1  A: 

Not that I'm aware of.

If so, it doesn't look like it would solve your issue (your issue appears to be that the program being started has different environment variables when programmatically launched then when manually launched).

STW
+2  A: 

Not so sure that it has anything to do with the command prompt. However, you get the exact same behavior as what you get from the command prompt by using the same command line interpreter. Use cmd.exe as the process file name, /c powercfg.exe as the argument.

Hans Passant
it doesn't solve my original problem, but it was worth a try
Jader Dias
A: 

It looks like the DLL is being loaded dynamically at runtime. You could find what the PATH variable is set to in the commandline then set this using the "control panel\system\advance\?environment?" or call setEnv or whatever the API is (think I might of had to pinvoke the win32 call when I did it last) to change the environment variable in your code.

Remember when working with system EnvVars that they are only read when you first run so you must typically restart apps to get the new settings.

Tony Lambert
A: 

Why not call the exe through the commandline:

using (var process = new Process
{
    StartInfo = new ProcessStartInfo
    {
         FileName = "cmd.exe",
         Arguments = "/C powercfg.exe",
         RedirectStandardOutput = true,
         UseShellExecute = false,
    }
})
{
    process.Start();
    process.WaitForExit();
}

It's a little unusual but it should work....

Tony Lambert
Hans beat be to it....
Tony Lambert
sorry if the example code didn't work as is BUT I copied YOUR code from YOUR original question... and I put the arguments in the with the Filename ;-)
Tony Lambert
I corrected it for you, since the arguments in the filename don't work, and it was missing a parenthesis
Jader Dias
The idea was good though! -1 !!! does it work now?
Tony Lambert
The code runs, and is not a bad ideia for this question, but it doesn't solve my problem
Jader Dias