我正在尝试在通过单击按钮启动的控制台中显示文本。我想我需要输入控制台的路径,我把问号放在Process.Start(“????”)。如何找到控制台路径?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Process.Start("????");
Console.WriteLine("Adam");
Console.Read();
}
}
答案 0 :(得分:3)
代码:
string returnvalue = "";
// Starts the new process as command prompt
ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
info.UseShellExecute = false;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
// Makes it so the command prompt window does appear
info.CreateNoWindow = true;
using (Process process = Process.Start(info))
{
StreamWriter sw = process.StandardInput;
StreamReader sr = process.StandardOutput;
// This for loop could be used if you had a string[] commands where each string in commands
// is it's own command to write to the prompt. I chose to hardcode mine in.
//foreach (string command in commands)
//{
// sw.WriteLine(command);
//}
sw.WriteLine("cd " + processPath);
sw.WriteLine("perl process.pl");
sw.Close();
returnvalue = sr.ReadToEnd();
}
return returnvalue;
答案 1 :(得分:1)
您需要执行应用程序cmd.exe
。但是使用Controle.WriteLine
将不会写入该控制台,并且Console.ReadLine
将无法从该控制台读取。您必须重定向流程的输入和输出流,以便与启动的控制台应用程序进行交互。
答案 2 :(得分:1)
这是一个包装AllocConsole()的类:
/// <summary>Simple class to allow creation and destruction of Consoles.</summary>
public static class ConsoleManager
{
#region public static Methods
/// <summary>
/// Creates a console output window, if one doesn't already exist.
/// This window will receive all outputs from System.Console.Write()
/// </summary>
/// <returns>
/// 0 if successful, else the Windows API error code from Marshal.GetLastWin32Error()
/// </returns>
/// <remarks>See the AllocConsole() function in the Windows API for full details.</remarks>
public static int Create()
{
if (AllocConsole())
{
return 0;
}
else
{
return Marshal.GetLastWin32Error();
}
}
/// <summary>
/// Destroys the console window, if it exists.
/// </summary>
/// <returns>
/// 0 if successful, else the Windows API error code from Marshal.GetLastWin32Error()
/// </returns>
/// <remarks>See the FreeConsole() function in the Windows API for full details.</remarks>
public static int Destroy()
{
if (FreeConsole())
{
return 0;
}
else
{
return Marshal.GetLastWin32Error();
}
}
#endregion // public static Methods
#region Private PInvokes
[SuppressMessage( "Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage" ), SuppressUnmanagedCodeSecurity]
[DllImport("kernel32.dll",SetLastError=true)]
[return: MarshalAs( UnmanagedType.Bool )]
static extern bool AllocConsole();
[SuppressMessage( "Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage" ), SuppressUnmanagedCodeSecurity]
[DllImport("kernel32.dll",SetLastError=true)]
[return: MarshalAs( UnmanagedType.Bool )]
static extern bool FreeConsole();
#endregion // Private PInvokes
}
只需调用ConsoleManager.Create(),然后就可以执行Console.WriteLine()。
答案 3 :(得分:1)
您应该有两个项目。第一个是具有所有功能的 Windows应用程序,另一个应该是类型为“控制台应用程序”的项目。然后,您应该在按钮的单击事件中执行第二个项目(您的控制台application.exe)的输出。
问题是你没有那种方式调用“Console.WriteLine
”。简单地说它不起作用。我的建议是使用.NET Remoting在两个不同的项目之间完成工作。
.NET Remoting IPC:
希望它有所帮助!
答案 4 :(得分:1)
您需要做的是从Windows API获取控制台。这将创建一个可以输出和读取的控制台应用程序的新实例。
public partial class Form1 : Form
{
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern int AllocConsole();
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern int FreeConsole();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int alloc = AllocConsole(); // Grab a new console to write to
if (alloc != 1)
{
MessageBox.Show("Failed");
return;
}
Console.WriteLine("test");
Console.WriteLine("Adam");
string input = Console.ReadLine();
Console.WriteLine(input);
// Do other funky stuff
// When done
FreeConsole();
}
}