4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

C#.NETでコマンドプロンプトのコマンド実行して標準出力(レスポンス)の文字列取得

Posted at

動作確認サンプル

image.png

image.png

サンプルコード
using System;
using System.Diagnostics;

namespace Komakoma
{
    internal class Program
    {
        static void Main(string[] args)
        {

            ProcessStartInfo process_start_info_ = new ProcessStartInfo();

            process_start_info_.FileName = "cmd";
            const string COMMAND_ = "ipconfig /?";

            process_start_info_.Arguments = "/c " + COMMAND_;

            //コンソール開かない。
            process_start_info_.CreateNoWindow = true;

            //シェル機能使用しない。
            process_start_info_.UseShellExecute = false;

            //標準出力をリダイレクト。
            process_start_info_.RedirectStandardOutput = true;

            Process process_ = Process.Start(process_start_info_);

            //標準出力を全て取得。
            string res_ = process_.StandardOutput.ReadToEnd();

            process_.WaitForExit();
            process_.Close();

            //取得した標準出力を表示。
            Console.WriteLine(res_);

            //結果確認の為に入力待ちで止める。
            Console.ReadLine();

        }
    }
}

実行結果は下記。
image.png

参考サイトさん

バージョン

Windows 10 Pro 22H2 OSビルド 19045.2673
Microsoft .NET Framework Version 4.8.04084

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?