0

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?

Community
  • 1
  • 1
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
  • You will be better served to understand what the real problem is and fix that instead of looking for crazy hacks and workarounds. For one thing, it's hard to work around something when you don't know what or where the something is. You're just stabbing at the dark. – dthorpe Sep 29 '10 at 16:29
  • @dthorpe I wouldn't if I could – Jader Dias Sep 29 '10 at 16:47
  • Well, don't give up on your previous question just yet. You only asked the question 4 hours ago. You won't have full benefit of all of your global stackoverflow pool of experts for at least 24 hours. ;> – dthorpe Sep 29 '10 at 17:19
  • @dthorpe it was already solved, but the cause of the problem or the solution are still unknown – Jader Dias Sep 29 '10 at 17:50

4 Answers4

2

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
  • 922,412
  • 146
  • 1,693
  • 2,536
1

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
  • 44,917
  • 17
  • 105
  • 161
0

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.

AnthonyLambert
  • 8,768
  • 4
  • 37
  • 72
0

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....

Jader Dias
  • 88,211
  • 155
  • 421
  • 625
AnthonyLambert
  • 8,768
  • 4
  • 37
  • 72
  • 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 ;-) – AnthonyLambert Sep 29 '10 at 16:41
  • I corrected it for you, since the arguments in the filename don't work, and it was missing a parenthesis – Jader Dias Sep 29 '10 at 16:45
  • The code runs, and is not a bad ideia for this question, but it doesn't solve my problem – Jader Dias Sep 29 '10 at 16:55