0

This ticket is not a duplicate of this popular question since the solution there does not fix the problem. Rather I think it has to do with calling a file that's residing in the %WINDIR%\System32 directory, because calling a batch file with anything else seems to work.

I have a batch file with the the following contents:

telnet 10.147.36.20 11211
pause

I kick it off by executing the following code:

var psi = new ProcessStartInfo(entry.ExecutablePath);
Process.Start(psi);

I get the following:

'telnet' is not recognized as an internal or external command,
operable program or batch file.

even though if I execute telnet 10.147.36.20 11211 from the command line, it works perfectly fine.

I've tried telnet.exe, c:\windows\system32\telnet.exe, start telnet.exe and other variations, but nothing seems to work.

What am I missing?

AngryHacker
  • 59,598
  • 102
  • 325
  • 594
  • If you are running a batch file, the process you start should be `cmd.exe` and pass the batch file as a parameter. Can you tell us exactly what `entry.ExecutablePath` contains? Just the path to the `.bat` file? – Ron Beyer Apr 09 '18 at 18:34
  • 1
    Telnet is not installed by default on Windows since Windows Vista and Server 2008. – Squashman Apr 09 '18 at 18:34
  • What does `entry.ExecutablePath` contain? – torsan Apr 09 '18 at 18:34
  • Process.Start(@"cmd", @"/c ""path_to_bat"""); – pagratios Apr 09 '18 at 18:37
  • @AngryHacker ensure that your bat file is in ansi encoding. For example, open in notepad, choose save as and choose Encoding: ANSI. Or at least ensure it's now not in UTF-8 with BOM, I have a suspicion that might be the reason. – Evk Apr 09 '18 at 19:32
  • Before running your batch file for the first time, you need to execute this one-time setup that installs telnet on the destination machine: `dism /online /Enable-Feature /FeatureName:TelnetClient`. After executing that command, you should be able to execute your batch file. – Icemanind Apr 09 '18 at 19:52
  • @RonBeyer I tried `cmd.exe /c c:\foobar\batch.bat`, per the linked question - same results. – AngryHacker Apr 09 '18 at 20:50
  • @Squashman I installed it. That's why when I run the same batch file from the command line, it works perfectly fine. – AngryHacker Apr 09 '18 at 20:50
  • @torsan `entry.ExecutablePath = C:\Users\Frank.Rizzo\Desktop\Links\clear2.bat` – AngryHacker Apr 09 '18 at 20:51
  • @pagratios I did - the results were exactly the same. I think it has something to do with commands being in the system32 directory than anything else. – AngryHacker Apr 09 '18 at 20:53
  • @Evk I was really hoping that was the answer, because the file was indeed in UTF-8 (without BOM). Unfortunately converting it to ANSI did not fix the problem. – AngryHacker Apr 09 '18 at 20:56
  • Well then I'm out of ideas. I ran your code with utf8 bat file (by accident) and it produced error like yours, but when I fixed that it ran just fine. And if you run bat (not just telnet but your bat) from command line - it runs fine? – Evk Apr 09 '18 at 20:57
  • @Icemanind It was already installed, which is why I can run the telnet from command line – AngryHacker Apr 09 '18 at 20:58

1 Answers1

0

When you are calling:

ProcessStartInfo(entry.ExecutablePath); 

it looks wrong. You're passing in a path without the executable name.

Try:

FileInfo oFileInfo = new FileInfo(entry);
ProcessStartInfo psi = new ProcessStartInfo(oFileInfo.FullName);
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • Please check [how to answer](https://stackoverflow.com/help/how-to-answer) Format this please – Morse Apr 09 '18 at 18:56