简体   繁体   中英

c# opens the batch file but doesn't execute the VBScript in the .bat

I'm developing an application in C#. The main idea is:

  1. Press button
  2. Open .bat file
  3. The .bat file opens Telnet [IP] [Port]
  4. .bat file executes the VBScript
  5. 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:

Is it possible to use a batch file to establish a telnet session, send a command and have the output written to a file?

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.

What you could do is find the cscript.exe file and execute it directly,like this:

for /f "tokens=*" %%C in ('where csscript.exe') do (%%C Sendkeys.vbs&goto Next)
:Next
rem This is only necessary because we want to run the script once

This migth work, but as the commenters said,using sendkeys is really not recommended,because it can cause major problems, once the windows are set incorrectly, or some of the progresses get delayed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM