Отправка команды в открытую cmd.exe - C#
Формулировка задачи:
Здравствуйте, необходимо отправить в ЗАРАНЕЕ ОТКРЫТУЮ кмд команду. если вообще возможно.
Актуально, не могу найти нигде, помогите пожалуйста, я уже не могу)
Просто великолепные ответы... я не знаю уже где искать... думал хоть вы поможете
Решение задачи: «Отправка команды в открытую cmd.exe»
textual
Листинг программы
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace AttachConsoleSample
{
static internal class Program
{
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool AttachConsole(uint dwProcessId);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool FreeConsole();
[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwPocessId);
static private void Main(string[] args)
{
IntPtr hWnd = FindWindow("ConsoleWindowClass", "Командная строка");
if (IntPtr.Zero == hWnd)
{
ShowErrorMessage(new Win32Exception(Marshal.GetLastWin32Error()).Message);
return;
}
uint processId;
GetWindowThreadProcessId(hWnd, out processId);
if (0 == processId)
{
ShowErrorMessage(new Win32Exception(Marshal.GetLastWin32Error()).Message);
return;
}
bool result = AttachConsole(processId);
if (false == result)
{
ShowErrorMessage(new Win32Exception(Marshal.GetLastWin32Error()).Message);
return;
}
//
// Присоединились к консоли.
// Делаем здесь что нужно
//
result = FreeConsole();
{
ShowErrorMessage(new Win32Exception(Marshal.GetLastWin32Error()).Message);
return;
}
}
static private void ShowErrorMessage(string message)
{
MessageBox.Show(message, "AttachConsoleSmaple", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}