I'm developing an application in C#. The main idea is:
- Press button
- Open .bat file
- The .bat file opens Telnet [IP] [Port]
- .bat file executes the VBScript
- VBScript writes some commands to the telnet window
When I run this batch file by double-clicking on it, it works fine. However, wen I try to run it from a C# app, it doesn't work.
I already tried many methods.
Here is a few examples about what I tried:
C# Winforms and command line batch files
batch works fine but "windows cannot find devcon.exe" in c# program
When I tried this:
string cmd = @"path";
var m_command = new System.Diagnostics.Process();
m_command.StartInfo.FileName = @"file.bat";
m_command.StartInfo.Arguments = cmd;
m_command.Start();
I got the error:
windows cannot find .exe make sure you typed the name correctly and then try again
And when I tried this:
string cmd = @"path";
var m_command = new System.Diagnostics.Process();
m_command.StartInfo.FileName = @"file.bat";
m_command.StartInfo.Arguments = cmd;
m_command.Start();
It works, but just opens telnet, the VBScript doesn't works.
This is the code in the .bat file:
:: Open a Telnet window
start C:\Windows\System32\telnet.exe 192.168.0.198 49211
:: Run the script
cscript SendKeys.vbs
This is the code in the .svs file:
set OBJECT=WScript.CreateObject("WScript.Shell")
WScript.sleep 500
OBJECT.SendKeys "T{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "T{ENTER}"
OBJECT.SendKeys " "
I expect the .VBS to write the command to the telnet window, however when I click the button in c# form, it just opens telnet, the VBScript doesn't works.
SendKeysfor automating the Windowstelnetcommand. Instead use atelnetcommand that is actually scriptable, e.g.plinkfrom the PuTTY suite. - Ansgar WiechersSendKeysrequires an interactive GUI (can't use it in a background process), and it will send keystrokes to whatever is the active window. Which may or may not be what you think it is. It's very unreliable and should not be used unless there are absolutely no other ways of dealing with the problem at hand. And perhaps not even then. - Ansgar Wiechers