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

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

2 つのプロジェクトが必要です。1 つはすべての機能を備えたWindows アプリケーションで、もう 1 つは「コンソール アプリケーション」タイプのプロジェクトである必要があります。次に、ボタンのクリック イベントで 2 番目のプロジェクト (Your Console application.exe) の出力を実行する必要があります。

問題は、そのように " " と呼ぶものがないことですConsole.WriteLine。単に機能しません。私のお勧めは、.NET Remoting を使用して、2 つの異なるプロジェクト間でスタッフを処理することです。

.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 に答える
1

アプリケーションを実行する必要がありますcmd.exe。ただし、使用Controle.WriteLineしてもそのコンソールには書き込まれConsole.ReadLineず、そのコンソールからは読み取られません。プロセスの入力ストリームと出力ストリームをリダイレクトして、開始されたコンソール アプリケーションと対話する必要があります。

于 2012-12-14T13:02:43.783 に答える