C#中的控制台路径是什么?
我试图在从单击按钮启动的控制台中显示文本。我想我需要输入控制台的路径,在这里我把问号放在进程中。开始(“??”)。如何找到控制台路径C#中的控制台路径是什么?,c#,path,console-application,C#,Path,Console Application,我试图在从单击按钮启动的控制台中显示文本。我想我需要输入控制台的路径,在这里我把问号放在进程中。开始(“??”)。如何找到控制台路径 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Process.Sta
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Process.Start("????");
Console.WriteLine("Adam");
Console.Read();
}
}
您需要执行应用程序
cmd.exe
。但是使用Controle.WriteLine
不会写入该控制台,而console.ReadLine
不会从该控制台读取。您必须重定向进程的输入和输出流,才能与启动的控制台应用程序交互。您需要执行应用程序cmd.exe
。但是使用Controle.WriteLine
不会写入该控制台,而console.ReadLine
不会从该控制台读取。您必须重定向进程的输入和输出流,才能与启动的控制台应用程序交互。下面是一个很好的示例:
代码:
下面是一个很好的例子: 代码:
下面是一个包装AllocConsole()的类:
///允许创建和销毁控制台的简单类。
公共静态类控制台管理器
{
#区域公共静态方法
///
///创建控制台输出窗口(如果还不存在)。
///此窗口将接收来自System.Console.Write()的所有输出
///
///
///0如果成功,则返回Marshal.GetLastWin32Error()中的Windows API错误代码
///
///有关详细信息,请参阅Windows API中的AllocConsole()函数。
公共静态int Create()
{
if(alloconsole())
{
返回0;
}
其他的
{
返回Marshal.GetLastWin32Error();
}
}
///
///销毁控制台窗口(如果存在)。
///
///
///0如果成功,则返回Marshal.GetLastWin32Error()中的Windows API错误代码
///
///有关详细信息,请参阅Windows API中的FreeConsole()函数。
公共静态int Destroy()
{
if(FreeConsole())
{
返回0;
}
其他的
{
返回Marshal.GetLastWin32Error();
}
}
#endregion//公共静态方法
#地区私家侦探
[SuppressMessage(“Microsoft.Security”、“CA2118:ReviewSuppressUnmanagedCodeSecurity”)、SuppressUnmanagedCodeSecurity]
[DllImport(“kernel32.dll”,SetLastError=true)]
[返回:Marshallas(UnmanagedType.Bool)]
静态外部布尔alloconsole();
[SuppressMessage(“Microsoft.Security”、“CA2118:ReviewSuppressUnmanagedCodeSecurity”)、SuppressUnmanagedCodeSecurity]
[DllImport(“kernel32.dll”,SetLastError=true)]
[返回:Marshallas(UnmanagedType.Bool)]
静态外部bool FreeConsole();
#endregion//Private PInvokes
}
只需调用ConsoleManager.Create(),然后就可以执行Console.WriteLine()。下面是一个包装AllocConsole()的类:
///允许创建和销毁控制台的简单类。
公共静态类控制台管理器
{
#区域公共静态方法
///
///创建控制台输出窗口(如果还不存在)。
///此窗口将接收来自System.Console.Write()的所有输出
///
///
///0如果成功,则返回Marshal.GetLastWin32Error()中的Windows API错误代码
///
///有关详细信息,请参阅Windows API中的AllocConsole()函数。
公共静态int Create()
{
if(alloconsole())
{
返回0;
}
其他的
{
返回Marshal.GetLastWin32Error();
}
}
///
///销毁控制台窗口(如果存在)。
///
///
///0如果成功,则返回Marshal.GetLastWin32Error()中的Windows API错误代码
///
///有关详细信息,请参阅Windows API中的FreeConsole()函数。
公共静态int Destroy()
{
if(FreeConsole())
{
返回0;
}
其他的
{
返回Marshal.GetLastWin32Error();
}
}
#endregion//公共静态方法
#地区私家侦探
[SuppressMessage(“Microsoft.Security”、“CA2118:ReviewSuppressUnmanagedCodeSecurity”)、SuppressUnmanagedCodeSecurity]
[DllImport(“kernel32.dll”,SetLastError=true)]
[返回:Marshallas(UnmanagedType.Bool)]
静态外部布尔alloconsole();
[SuppressMessage(“Microsoft.Security”、“CA2118:ReviewSuppressUnmanagedCodeSecurity”)、SuppressUnmanagedCodeSecurity]
[DllImport(“kernel32.dll”,SetLastError=true)]
[返回:Marshallas(UnmanagedType.Bool)]
静态外部bool FreeConsole();
#endregion//Private PInvokes
}
只需调用ConsoleManager.Create(),然后您就可以执行Console.WriteLine()。您应该有两个项目。第一个是具有所有功能的Windows应用程序,另一个应该是类型为“Console Application”的项目。然后,您应该在按钮的单击事件中执行第二个项目(您的Console应用程序.exe)的输出 问题是,您没有这样调用“
Console.WriteLine
”的功能。这根本不起作用。我的建议是使用.NETRemoting在两个不同的项目之间进行协作
.NET远程处理IPC:
希望有帮助 你应该有两个项目。第一个是具有所有功能的Windows应用程序,另一个应该是类型为“Console Application”的项目。然后,您应该在按钮的单击事件中执行第二个项目(您的Console应用程序.exe)的输出 问题是,您没有这样调用“
Console.WriteLine
”的功能。这根本不起作用。我的建议是使用.NETRemoting在两个不同的项目之间进行协作
.NET远程处理IPC: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;
/// <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
}
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();
}
}