[英]What is the Console path in C#?
我試圖在通過單擊按鈕啟動的控制台中顯示文本。 我想我需要輸入控制台的路徑,在該路徑中我已經打上了問號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();
}
}
這是一個很好的示例,該怎么做: http : //cboard.cprogramming.com/csharp-programming/130369-command-prompt-use-within-csharp-class-file.html#post973331
碼:
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;
您需要執行應用程序cmd.exe
。 但是使用Controle.WriteLine
不會寫入該控制台,而Console.ReadLine
不會從該控制台讀取。 您必須重定向流程的輸入和輸出流,以與啟動的控制台應用程序進行交互。
這是包裝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()。
您應該有兩個項目。 第一個是具有所有功能的Windows應用程序 ,另一個應該是類型為“ 控制台應用程序 ”的項目。 然后,您應該在按鈕的click事件中執行第二個項目(您的Console application.exe)的輸出。
問題是您沒有那種那樣調用“ Console.WriteLine
”的東西。 簡直是行不通的。 我的建議是使用.NET Remoting在兩個不同的項目之間進行工作。
.NET遠程IPC:
希望能幫助到你!
您需要做的是從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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.