Advertisement
Guest User

Send ctrl+c to a cmd.exe process in c#

a guest
Feb 8th, 2012
1,050
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. string XCopyArguments = """ + dir.FullName + "" "" + destination + "" /D /S /I /E";
  2. Process XCopyProcess = new Process();
  3. ProcessStartInfo XCopyStartInfo = new ProcessStartInfo();
  4. XCopyStartInfo.FileName = "CMD.exe ";
  5. XCopyStartInfo.RedirectStandardError = true;
  6. XCopyStartInfo.RedirectStandardOutput = true;
  7. XCopyStartInfo.RedirectStandardInput = true;
  8. XCopyStartInfo.UseShellExecute = false;
  9. XCopyStartInfo.CreateNoWindow = true;
  10. XCopyStartInfo.Arguments = " /D /c XCOPY " + XCopyArguments;
  11. XCopyProcess.EnableRaisingEvents = true;
  12. XCopyProcess.StartInfo = XCopyStartInfo;
  13. XCopyProcess.Start();
  14. XCopyProcess.WaitForExit(15000);
  15. int ExitCode = XCopyProcess.ExitCode;
  16. if (ExitCode > 0 & !XCopyProcess.HasExited)
  17. {
  18. XCopyProcess.Kill();
  19. }
  20. XCopyProcess.Dispose();
  21.  
  22. Declare Function GenerateConsoleCtrlEvent Lib "kernel32" ( _
  23. ByVal dwCtrlEvent As Integer, _
  24. ByVal dwProcessGroupId As Integer _
  25. ) As Integer
  26.  
  27. Private Const CTRL_C_EVENT As Integer = 0
  28.  
  29. Private Sub SendCtrlC()
  30. GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0)
  31.  
  32. ' send a Ctrl-C to this process
  33. GenerateConsoleCtrlEvent(CTRL_C_EVENT, currentpid)
  34.  
  35. ' send a Ctrl-C to the cmd process
  36. GenerateConsoleCtrlEvent(CTRL_C_EVENT, cmdpid)
  37. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement