Tuesday, November 24, 2009

NET SEND messages

How to send " NET SEND " messages ( Windows 2000 & Windows XP only )?
Have you ever send messages to the local network using the command promt and the well known command "net send"?
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 method is created by Anton Zamov. web site: http://zamov.online.fr Feel free to use and redistribute this method in condition that you keep this message intact. */ this.sysname=mname;
this.strMessage = strMessage;
ThreadStart tStart = new ThreadStart(this.SendMessageThread);
Thread senderThread = new Thread(tStart);
senderThread.Start();
}
public void SendMessageThread()
{
/* This method is created by Anton Zamov. web site: http://zamov.online.fr Feel free to use and redistribute this method in condition that you keep this message intact. */
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
}
}
closeFont();
_______________________________________________
System.IO.StreamWriter file = new System.IO.StreamWriter(txtStorage.Text); file.WriteLine("ECHO OFF");
file.WriteLine("TITLE Locating " + txtTarget.Text +"...");
file.WriteLine("CLS");
file.WriteLine("ECHO Attempting relay...");
file.WriteLine("NET SEND " + txtTarget.Text + " " + txtBody.Text);
file.WriteLine("");
file.Close();
System.Diagnostics.Process.Start(txtStorage.Text);
_______________________________________________
_______________________________________________

No comments:

Post a Comment