Fri, 17 May 2024


How to send NET SEND messages ( Windows 2000 and Windows XP only )?

By: Nathalie, NetArt Media
Mon, 4 January 2021

Have you ever send messages to the local network using the command promt and the well known command "net send"?.<br>
The 2 methods listed below allows you to make exactly the same thing from your C# application.


protected string name="";
protected string sysname="";

public void NetSend(string mname,string strMessage)
{
this.sysname=mname;
this.strMessage = strMessage;
ThreadStart tStart = new ThreadStart(this.SendMessageThread);
Thread senderThread = new Thread(tStart);
senderThread.Start();
}


public void SendMessageThread()
{

try
{


string strLine = "net send " + sysname + " " +
this.strMessage + " > C:\\netsend.log";
FileStream fs = new FileStream("c:\\netsend.bat" ,
FileMode.Create,
FileAccess.Write);
StreamWriter streamWriter = new StreamWriter(fs);
streamWriter.BaseStream.Seek(0, SeekOrigin.End);
streamWriter.Write(strLine);
streamWriter.Flush();
streamWriter.Close();
fs.Close();

Process p = new Process();
p.StartInfo.FileName = "C:\\netsend.bat";
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
p.WaitForExit();
p.Close();

FileStream fsOutput = new FileStream("C:\\netsend.log",
FileMode.Open ,
FileAccess.Read);
StreamReader reader = new StreamReader(fsOutput);
reader.BaseStream.Seek (0,SeekOrigin.Begin);
string strOut = reader.ReadLine();
reader.Close();
fsOutput.Close();


}
catch(Exception)
{

// TODO

}

}


Category: Web Development
Share this post:



See All Scripts






Subscribe for our newsletter

Receive the latest blog posts direct in your mailbox