简体   繁体   English

C#中的控制台路径是什么?

[英]What is the Console path in C#?

I am trying to show text in a console launched from the click button. 我试图在通过单击按钮启动的控制台中显示文本。 I think I need to input the console's path where I have put the question marks Process.Start("????"). 我想我需要输入控制台的路径,在该路径中我已经打上了问号Process.Start(“ ????”)。 How do I find the console path? 如何找到控制台路径?

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Process.Start("????");
        Console.WriteLine("Adam");
        Console.Read();
    }
}

Here is a nice example how to do this: http://cboard.cprogramming.com/csharp-programming/130369-command-prompt-use-within-csharp-class-file.html#post973331 这是一个很好的示例,该怎么做: http : //cboard.cprogramming.com/csharp-programming/130369-command-prompt-use-within-csharp-class-file.html#post973331

Code: 码:

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;

You need to execute the application cmd.exe . 您需要执行应用程序cmd.exe But using Controle.WriteLine won't write to that console and Console.ReadLine won't read from that console. 但是使用Controle.WriteLine不会写入该控制台,而Console.ReadLine不会从该控制台读取。 You'll have to redirect the input and output streams of the process to interact with the console application started. 您必须重定向流程的输入和输出流,以与启动的控制台应用程序进行交互。

Here's a class that wraps AllocConsole(): 这是包装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
}

Just call ConsoleManager.Create() and then you should be able to do Console.WriteLine(). 只需调用ConsoleManager.Create(),然后您就可以执行Console.WriteLine()。

You should have two projects. 您应该有两个项目。 The first one is your Windows applications with all it's functionalities and the other one should be a project with type " Console Application ". 第一个是具有所有功能的Windows应用程序 ,另一个应该是类型为“ 控制台应用程序 ”的项目。 Then you should execute the output of the second project (Your Console application.exe) in the click event of the button. 然后,您应该在按钮的click事件中执行第二个项目(您的Console application.exe)的输出。

The problem is that you don't have the thing to call " Console.WriteLine " that way. 问题是您没有那种那样调用“ Console.WriteLine ”的东西。 Simply it doesn't work. 简直是行不通的。 My recommendation is to use .NET Remoting to do staffs between two different projects. 我的建议是使用.NET Remoting在两个不同的项目之间进行工作。

.NET Remoting IPC: .NET远程IPC:

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

Hope it helps! 希望能帮助到你!

What you need to do is grab the console from the windows API. 您需要做的是从Windows API中获取控制台。 This will create a new instance of a console application that can output and read etc. 这将创建一个控制台应用程序的新实例,该实例可以输出和读取等。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM