LoginSignup
0
2

More than 5 years have passed since last update.

C# で MSG コマンドを実行する

Last updated at Posted at 2017-06-11

C# で MSG コマンドを実行するサンプルコードです。
数時間はまったのでメモっておきます。

ポイントは ProcessStartInfo.FileName に指定する値。

それから、MSGコマンドはエラーがあるとStandardErrorにエラーメッセージを出力するので、それをチェックするようにしています。

public void SendMessage(string account, string computer, string message)
{
    var startInfo = new ProcessStartInfo
    {
        FileName = @"C:\windows\sysnative\msg.exe",
        Arguments = string.Format("{0} /SERVER:{1} \"{2}\"", account, computer, message),
        RedirectStandardError = true,
        CreateNoWindow = true,
        UseShellExecute = false,
    };
    var p = Process.Start(startInfo);
    var errorMessage = p.StandardError.ReadToEnd();
    p.WaitForExit();
    p.Close();

    if (errorMessage.Length > 0)
    {
        // エラー発生時の処理
    }
}
0
2
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
0
2