1

I'm trying to script multiple remote desktop connections in a Winform .NET 4.0 in C#.

Given a list of server names in _serverList,

        var startInfo = new ProcessStartInfo
        {
            FileName = "cmd.exe",
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = false
        };

        var process = new Process { StartInfo = startInfo };

        process.Start();

        foreach (string server in _serverList)
        {

            process.StandardInput.WriteLine(@"mstsc.exe /v:" + server);         
        }

When I run it on my local desktop (Windows 7) it works perfectly fine: all servers are launched, but once i port the application onto the server (Windows Server 2003 r 2, and the event that triggers this occurs, I get the error

"mstsc.exe" was not recognized as an internal or external command, operable program or batch file

I attempted variations of providing the full path of mstsc.exe, changing FileName = "mstsc.exe" and providing the server names as Arguments, but none work.

When I launch cmd.exe on the server, and manually input "mstsc.exe /v: someservername", the behaviour is as expected and the proper servers are launched.

Any insight to what is going wrong would be appreciated

Kevin Zhou
  • 1,273
  • 4
  • 17
  • 25

2 Answers2

0

When UseShellExecute is set to false, you must provide the full path to the executeable when setting the FileName property, environment variables don't function properly. Search for the location of "mstsc.exe" on Windows server 2003 R2. Do the same thing for Windows 7. These paths are likely to differ, which would explain the problem.

Use a helper class such as OperatingSystem to selectively choose the correct path based on such differences as OS, processor (E.G. x86 directories)), %systemroot% environment differences etc. You can then set the proper values in an app.config.

P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
  • 1
    Doing the exact path of mstsc.exe on the server was one of the things I've attempted before, when replacing "mstsc.exe" with "C:\Windows\system32\MSTSC.exe" yields the same problems – Kevin Zhou Jun 28 '12 at 15:40
  • @KevinZhou - Be sure you set this value to the FileName property and that you use quotes around any paths that contain a space, including parameters. The error indicates a bad path. You simply need to figure out why the path is bad. – P.Brian.Mackey Jun 28 '12 at 15:44
0

I was able to run the code you provided just fine on Windows Server 2012 R2. It's possible that there might be some security mechanism in the OS at play here. Have you tried elevating privileges on your executable manually yet?

If that works you can look into this post:

Elevating process privilege programmatically?

Community
  • 1
  • 1
Residualfail
  • 79
  • 1
  • 12