I just started using C# again and I encountered a bug/issue while trying to start a process using command prompt.
I am trying to start/open the default On-Screen Keyboard from windows using command prompt. However, after the osk.exe executed, The command prompt window is still open. After manually closing the command prompt window by clicking "x" button and tried to click the button to execute the code again, The command prompt will then close after opening osk.exe as it should be.
Here is the code I tried to run:
Process.Start(Path.Combine(Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.System)).FullName, "Sysnative", "cmd.exe", "/c osk.exe ; exit");
I got this code from this website
I even tried to run another line just to kill all open command prompt with code below. But after executing, another command prompt is opened and never automatically closed....
System.Diagnostics.Process.Start("CMD.exe", "taskkill /IM cmd.exe");
I tried to manually run in command prompt just to see if my windows command prompt has bugs, but its working fine.
I just want to open on-screen keyboard when a button is clicked without keeping an open command prompt and without changing the platform type of my application (i tried to do this before and it messed up most of my OLEDB Connections).
Any answers and suggestions is highly appreciated.
Also, I'm very sorry for my bad English.
This should work, it doesn't start CMD though:
//specify full path - possible through environment variables
var psi = new ProcessStartInfo(#"c:\windows\system32\osk.exe");
psi.UseShellExecute = true;
var process = Process.Start(psi);
Related
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:
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.
I created a cmd process in which I display the output to a window. I would like to be able to terminate the command like pressing Control-c in the command prompt.
new ProcessStartInfo("cmd.exe");
The only option I can think of is to terminate the process. Is this what pressing control-c does? The problem with this is some user settings will be lost. Two I can think of are prompt and the current directory. could there be any others? I can remember these and create a new process and reset them. Or is there another way to simulate the interruption?
enter image description here
You can start a process by calling the Start method. You can also end the process by calling the Kill method. All well and good. Now lets start a process by running cmd.exe and redirecting the input and output. So now assume we execute a command that takes a long time. It would be nice to cancel the command like we can do in a DOS window (Command Prompt) instead of forcing the user to close and reopen the app losing their history in the process.
I can provide more details if necessary.
The mechanism that cmd.exe uses1 is GenerateConsoleCtrlEvent, and it is not available when your Process.Start call created a new console, because of this restriction in the documentation:
Only those processes in the group that share the same console as the calling process receive the signal. In other words, if a process in the group creates a new console, that process does not receive the signal, nor do its descendants.
The option you have is to not launch cmd.exe directly, but start a console program that is in turn responsible both for starting cmd.exe in its shared console, and also keeping a communication link with your program open in order to know when to generate console control events.
Actually it should be the console subsystem detecting Ctrl+C and generating the console event, and it's received by both cmd.exe and any child process. cmd.exe has a complex handler that will cancel a running builtin command (like dir) and not exit. Child processes may also have logic to exit cleanly and save state when the console control eventis seen.
i am trying to run the following cmd command, that works on the CMD, from a C# console app but nothing happens:
string strCmdText = "\\office\\Public\\Tools\\myTool\\myTool_V1.0\\myTool.exe -kan tools -kdb Adhoc - ktn Components3 - uri https://coprime.osdinfra.net";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
the cmd window is emmidiatly closed after pressing F5 in VS so i can't see the output of "myTool.exe" - which does in fact print status about its progress on the cmd when run from a cmd window.
Also the desired effect of the program doesn't happen so i know it didn't work.
Need help please
Command Prompt does not take a program in as an argument to start. However, I can't see a reason to use command prompt here. You're code is starting the process "Cmd.exe" so it can start another process. Why not eliminate the middle man and just start the process you want to start? Then you can pass the process' real arguments as arguments in process.start().
Update:
You can start a program from command prompt, but it's a specific command. It goes like this:
CMD.exe /c {string of commands to execute}
So for instance, you could run it through cmd if you need to by doing this:
string strCmdText = "/c start \\office\\Public\\Tools\\myTool\\myTool_V1.0\\myTool.exe -kan tools -kdb Adhoc - ktn Components3 - uri https://coprime.osdinfra.net";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
I was attempting to do a Windows command prompt re-code in C#. I was wondering how the command prompt knows when to wait for the process started to exit, and when not to wait for the called process to exit.
For example, if you type in the command prompt "notepad", Notepad will launch, but you can still execute other commands. However, if you open a utility such as more.com, ping.exe, or another utility, it will wait for the executing program to finish before letting you execute another command.
How does the command prompt know when to wait for exit, and how can this behavior be emulated in C#?
If the application is a Win32 GUI application, it will just run and command prompt won't wait for it to exit.
If the application is a console application, it will run in the command prompt and you'll need to wait for it to finish to get the command prompt back.
EDIT:
OK. It seems you need technical explanation. If you want to emulate the same feature in your application, you can check IMAGE_OPTIONAL_HEADER of EXE files here.
Inside IMAGE_OPTIONAL_HEADER, there is:
WORD Subsystem;
If SubSystem == 0x02 it means it's a GUI application.
If SubSystem == 0x03 it means it's a command prompt app.
EDIT 2:
If you want to see it in action:
Download http://www.ntcore.com/exsuite.php
Copy calc.exe or notepad.exe to your desktop
Open copied calc.exe in CFF Explorer
Navigate to Nt Headers -> Optional Headers
Change SubSystem from 0x002 to 0x003
Save it
Now run the new modified calc and you'll see the command prompt wait for it to be terminated.
The default from a command prompt is to spawn the GUI program in a separate process/fork.
However, you can run notepad (or other GUI program) inline with your command prompt / shell script:
start /w notepad.exe
This way, the command prompt / shell script only continues after the notepad.exe process has terminated.
Hope this helps, TW
When you start a new process, a GUI application in this context that actually works outside the boundaries of the prompt, the prompt will not wait. But, if you run a command that works entirely under the boundaries of the current instance of a prompt, it waits.
So, command notepad just starts the Notepad application and leaves control of application. While, command ipconfig runs under a domain (no this is not application domain), of the prompt.
To extremely generalize, when you use Process.Start in your C# equivalent, do not wait. It anyway will not.
I am using Selenium PhantomJS. When my program starts it opens command prompt (cmd) which then turns on the browser and does everything I have programmed it to do.
According to this answer there is no way to hide the command prompt (until v2.4 gets released), so I want to ask if there is a way to at least start it minimized or maybe stop Windows from focusing it after it pops up.
I tried the following code to programmatically press Alt+Tab:
IWebDriver driver = new PhantomJSDriver(); //this starts browser and opens cmd
System.Windows.Forms.SendKeys.Send("%{TAB}");//alt+tab
Unfortunately this only freezes the mouse as if I am in endless loop until the program ends.
I am using Windows 7, VS2013.
Well, the new version 2.40 is out, so here is the solution - https://stackoverflow.com/a/21949015/1115382 - credit goes to #Martin Su.