1

我正在尝试在从单击按钮启动的控制台中显示文本。我想我需要输入我放置问号 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();
    }
}
4

5 回答 5

3

这是一个很好的例子: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;
于 2012-12-14T13:04:10.443 回答
1

您需要执行应用程序cmd.exe。但是 usingControle.WriteLine不会写入该控制台,Console.ReadLine也不会从该控制台读取。您必须重定向进程的输入和输出流以与启动的控制台应用程序进行交互。

于 2012-12-14T13:02:43.783 回答
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()。

于 2012-12-14T13:04:29.000 回答
1

你应该有两个项目。第一个是具有所有功能的Windows 应用程序,另一个应该是类型为“控制台应用程序”的项目。然后您应该在按钮的单击事件中执行第二个项目(您的控制台 application.exe)的输出。

问题是你没有这样称呼“ Console.WriteLine”的东西。简直是行不通。我的建议是使用 .NET Remoting 在两个不同的项目之间做人员。

.NET 远程 IPC:

http://www.codeguru.com/csharp/csharp/cs_syntax/remoting/article.php/c9251/NET-Remoting-Using-a-New-IPC-Channel.htm

希望能帮助到你!

于 2012-12-14T13:07:10.623 回答
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();
    }
}
于 2012-12-14T13:09:34.950 回答