C#调用net.exe发送消息

默认情况下,WinXP SP2的Messenger服务是禁止的,如果需要使用必须先启动该服务。

        //发消息
        private void NetSendMsg(string strIp, string strHostname, string strMsg)
        {
            try
            {
                Process sendprocess =new  Process();
                sendprocess.StartInfo.FileName = "net.exe";
                sendprocess.StartInfo.Arguments = "send " + strIp + " " + strMsg;
                sendprocess.StartInfo.UseShellExecute = false;
                sendprocess.StartInfo.RedirectStandardOutput = true;
                sendprocess.StartInfo.RedirectStandardError = true;
                sendprocess.StartInfo.CreateNoWindow = true;
                sendprocess.Start();

                string strSend = sendprocess.StandardOutput.ReadToEnd();

                if (strSend.IndexOf("消息已经送到 "+strIp+"。") != -1)
                {
                    lb_Info.Items.Add(DateTime.Now.ToLongTimeString() + "发消息到 " + strHostname + ":" + strMsg);
                }
                else
                {
                    lb_Info.Items.Add("发消息到" + strHostname + "失败!"+DateTime.Now.ToLongTimeString());
                }
               
            }
            catch(Exception ex)
            {
                lb_Info.Items.Add(ex.Message+DateTime.Now.ToLongTimeString()+"发消息到" + strHostname + "失败!");
            }
        }

原文地址:https://www.cnblogs.com/mossan/p/749664.html