I have already switch on the telnet option on control panel - program/features. Though telnet worked fine manually, but when I run below from my C# library, I am getting this error :
"'telnet' is not recognized as an internal or external command, operable program or batch file."
This is the code I use to start Telnet:
Process.Start("cmd", "/k telnet")
What makes the difference between this and running it from the command prompt manually and how do I overcome this difference?
On installing Telnet Client via Control Panel - Programs and Features - Turn Windows features on or off there is installed telnet.exe
into directory %SystemRoot%\System32
.
The directory %SystemRoot%\System32
is for 64-bit applications on 64-bit Windows. Therefore the installed executable telnet.exe
is also the 64-bit version. This directory contains also the 64-bit version of cmd.exe
.
System environment variable PATH contains %SystemRoot%\System32
which is responsible for finding %SystemRoot%\System32\cmd.exe
and %SystemRoot%\System32\telnet.exe
when being executed without file extension and without path.
But there is additionally the directory %SystemRoot%\SysWOW64
for 32-bit applications containing the 32-bit versions of the executables.
Microsoft explains with the documentation pages WOW64 Implementation Details and File System Redirector how a file system access to %SystemRoot%\System32
by a 32-bit application is redirected automatically to %SystemRoot%\SysWOW64
.
There is no 32-bit version of telnet.exe
installed into %SystemRoot%\SysWOW64
on installing Telnet Client.
So what happens on:
Process.Start("cmd", "/k telnet")
When the C# library is compiled as 64-bit library used by a 64-bit application, 64-bit Windows finds and starts %SystemRoot%\System32\cmd.exe
which finds and then starts %SystemRoot%\System32\telnet.exe
.
But when the C# library is compiled as 32-bit library used by a 32-bit application, 64-bit Windows finds and starts via file system redirector %SystemRoot%\SysWOW64\cmd.exe
which cannot find telnet.*
with a file extension listed in environment variable PATHEXT in current directory or any directory of environment variable PATH because there is no file telnet.exe
in directory %SystemRoot%\SysWOW64
.
The best solution is definitely to use a static (or dynamic) telnet library in the C# library to be independent on telnet.exe
as suggested by Panagiotis Kanavos. In my point of view it is a shame for every C# programmer using external executables via a process call for which C# code can be quite easily also written by the programmer. Using any world wide web search engine with the search term C# telnet
returns lots of pages with solutions, for example C# Telnet Library on Stack Overflow.
Of course it is also possible to get first value of environment variable SystemRoot using GetEnvironmentVariable method to get path to Windows directory, or even better using GetWindowsDirectory or GetSystemWindowsDirectory method.
Then concatenate this string value with "\\System32\\telnet.exe"
to a new string and check if the file with that full path exists using File.Exists method. If that file exists on 32-bit Windows with 32-bit version of telnet.exe
installed if C# application is also a 32-bit application or on 64-bit Windows with 64-bit telnet.exe
installed if C# application is a 64-bit application, then this file name with full path and file extension can be used in a process call.
Otherwise concatenate the Windows directory path with "\\Sysnative\\telnet.exe"
and check if file with that full path exists. If this is true on 64-bit Windows with 64-bit version of telnet.exe
installed if C# application is a 32-bit application, it would be possible to run from within 32-bit application the 64-bit telnet client executable using this path.
But if that fails also, telnet.exe
is not installed at all which is the reason why usage of telnet.exe
from within a C# code application is in general not advisable.
Honestly, I don't understand why to have code in a C# library which just starts a command process executing telnet.exe
without options and therefore requiring user input and which keeps the command process running after telnet client session terminated. The C# library function can be replaced by a shortcut to telnet.exe
on Windows desktop or in Windows start menu of the user.