0

I used C# with a console program to create a new cmd process, did not redirect stdin or stdout, so I could type into the command line from here. (I was having trouble using telnet from there, so this step was just an investigation.) Able to type into the window and receive output. When I switched to c:Windows\system32, typing dir te*.exe shows nothing. In another command prompt I created directly, I see the file (telnet.exe). Any suggestions about what is wrong?

    {

        ProcessStartInfo startInfo = new ProcessStartInfo(@"cmd.exe"); 
        startInfo.UseShellExecute = false;
        startInfo.CreateNoWindow = true;
        startInfo.WindowStyle = ProcessWindowStyle.Normal; 
        startInfo.CreateNoWindow = false;
        startInfo.Arguments = host;
        using (Process p = new Process())
        {
            p.StartInfo = startInfo;
            p.Start();
        }
    }
  • Try typing `where telnet` into your command prompt - it should tell you where your executable is located. What is the result? – Ian R. O'Brien Jul 14 '16 at 20:12
  • Running in Windows 7 - forgot to mention this. – J Jay Meyer Jul 14 '16 at 20:15
  • C:\Windows\system32>where telnet INFO: Could not find files for the given pattern(s). – J Jay Meyer Jul 14 '16 at 20:19
  • 1
    You can change telnet config in "Turn Windows Features on or off" check this article https://social.technet.microsoft.com/wiki/contents/articles/910.windows-7-enabling-telnet-client.aspx – Fatih Sert Jul 14 '16 at 20:20
  • From "normal" command prompt (BTW telnet itself works here too): H:\>where telnet C:\Windows\System32\telnet.exe H:\> – J Jay Meyer Jul 14 '16 at 20:21
  • It's a little confusing, but you're saying when you launch `cmd` by itself you can see telnet, but not when you launch the process through the C# code you have listed? That's what I see when I ran your code. It's interesting. – Ian R. O'Brien Jul 14 '16 at 20:23
  • I'm confused by what you mean when you say "here" and "there". What are those places – Erik Funkenbusch Jul 14 '16 at 20:26
  • 1
    Possible duplicate of [Process.Start("telnet.exe") throws exception](http://stackoverflow.com/questions/1710938/process-starttelnet-exe-throws-exception) – Ian R. O'Brien Jul 14 '16 at 20:29
  • You should be able to find your solution with the duplicate question link above. I was able to reproduce and resolve your issue following the steps. Short answer, change your build to x86 or x64 to match your system or run the app from C:\Windows\sysnative\ – Ian R. O'Brien Jul 14 '16 at 20:30

3 Answers3

3

Since Windows 7, I believe, you have to install Telnet as a Windows Feature.

Here you have a guide to enable Telnet on Win 7, but it's applicable to Win 8.1 as well as Windows 10.

Just in case you can't read the site, the steps are:

Go to Control Panel -> Programs -> Turn Windows Features on or off -> Scroll down until you find the Telnet Client option

  • I believe this article is identical to my problem: http://stackoverflow.com/questions/33984146/c-sharp-new-process-created-cannot-access-certain-files?rq=1 – J Jay Meyer Jul 14 '16 at 20:33
0

Based on the above article, looked at project build properties. Platform target was set to x86. Changing to "Any CPU" at least allows me to see the program!

BTW I have looked for the answer for several days before posting this, but in the margin in related - "C# New process created cannot access certain files" gave me the info - after I created this question

Thanks, heuristics!

  • hah! that's what I get for taking so long to type my response. I'm glad you found the answer. – seairth Jul 14 '16 at 21:21
  • 1
    You can use the following to always get the system native system32 bit folder, nomatter the combination of 32 bit or 64 bit process or OS `string sysFolder64Bit; if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess) { sysFolder64Bit = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "sysnative"); } else { sysFolder64Bit = Environment.GetFolderPath(Environment.SpecialFolder.System); }` – Scott Chamberlain Jul 14 '16 at 21:41
  • The `sysnative` folder is only visible when you are a 32 bit process on a 64 bit system. Any other combination it will not exist. – Scott Chamberlain Jul 14 '16 at 21:43
0

This is a really devious one. When you are using windows explorer or opening a command prompt directly, you are starting a 64-bit process. When you are starting the "cmd.exe" with Process.Start(), you will get the same version as the process that's starting it. In your case, you are creating a 32-bit process, so you get the 32-bit version of the command prompt. If you change your project to create target x64, you will see the files!

Why is this so? Because, depending on whether you are accessing System32 through a 32-bit or 64-bit app, you will actually be accessing different System32 folders! For more on this, follow this link:

https://superuser.com/questions/330941/some-files-in-system32-not-accessible-outside-explorer-on-windows-7

Community
  • 1
  • 1
seairth
  • 1,966
  • 15
  • 22