0

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");

enter image description here

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.

  • I tried it before and did not work. I have actually read so many reference similar here and I'm not sure about it, They said that that osk.exe cannot run at 32 bit. or maybe I'm wrong.... – Genesis Mallari Jul 05 '20 at 18:13
  • It looks like you're passing all those parameters to `Path.Combine` instead of just the parts that make up the path to the executable. Have you tried `Process.Start(Path.Combine(Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.System)).FullName, "Sysnative", "cmd.exe"), "/c osk.exe");`? – Rufus L Jul 05 '20 at 18:19
  • Thank you, I did tried it however it still open. I also tried Process.Start(@"c:\Windows\Sysnative\cmd.exe", "/c osk.exe"); and still get same issue. perhaps theres a bug with using Sysnative? – Genesis Mallari Jul 05 '20 at 18:28
  • Maybe so, did you try system32 instead? – Rufus L Jul 05 '20 at 19:08

1 Answers1

0

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);
Stefan
  • 17,448
  • 11
  • 60
  • 79
  • Thanks for the suggestion, however it did not work. I got error: System.ComponentModel.Win32Exception: 'The system cannot find the file specified' – Genesis Mallari Jul 05 '20 at 18:16
  • Oh, yes, sorry, you need to specify the path - I tested it in admin mode and then it's set correctly by default - let me know if it works - or I've got some path settings, because also without admin it works – Stefan Jul 05 '20 at 18:16
  • I tried changing it @"C:\WINDOWS\system32\osk.exe however it still shows same error. I did open file location of where the osk.exe is located and just copied it. maybe I did something wrong with the codes – Genesis Mallari Jul 05 '20 at 18:22
  • @GenesisMallari: Where is your `osk.exe` located; mine was in system32 – Stefan Jul 05 '20 at 18:26
  • hmm, its actually the same "C:\WINDOWS\system32". I actually tried to copy the osk.exe file on another drive (just to see if it works) but no surprised for it to fail haha. Perhaps it needs Administration rights to locate the file within system32? – Genesis Mallari Jul 05 '20 at 18:30
  • Strange indeed... here it 's working flawless.... you did set the UseShellExecute, right? – Stefan Jul 05 '20 at 18:39
  • yes I did tried that, I think I found the issue and its because I'm running my application as 32 bit. hence I cannot directly access osk.exe. perhaps I should just endure the small pain of closing cmd prompt forever hahaha....Thank you so much anyway – Genesis Mallari Jul 05 '20 at 18:49
  • I'll check the behaviour at my side as well – Stefan Jul 05 '20 at 18:50
  • @GenesisMallari: I am not able to test it, but the 32bit part seems unlikely - fact is: you're starting a new process. I doubt it will inherit the compatibility settings – Stefan Jul 05 '20 at 18:53