65.9K
CodeProject is changing. Read more.
Home

Sending Messages to Multiple Computers in the Workgroup/Network

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.96/5 (20 votes)

Oct 29, 2008

CPOL

1 min read

viewsIcon

74101

downloadIcon

5293

GUI tool for sending messages to multiple computers and see the message log.

Introduction

This tool helps you to send messages across different computers on your network. You can also view the sent/received messages. All messages are stored in the event log of your system.

Background

The tool uses the Messenger service of the Windows operating system. For running the software, you require .NET Framework 3.5 (which is available for download on the Microsoft site here).

Using the Code

The application is built with Visual Studio 2008 and in C# language. It consists of two forms; one for sending the message and another for viewing the log. The application uses the Windows Messenger service and it should be started before running the application. The event log of Windows application is used for logging the messages.

The sending message form mainly consists of Listview control (lvSendTo), textbox (txtMessage), two buttons (btSend and btView). When this form is loaded, computers on the network are searched and populated into the listview control. The code for doing that is given below:

//Namespaces
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;      
  
//Put the below code inside form load event          
// Finds the computers in the network
Process proc = new Process();
proc.StartInfo.FileName = "net.exe";
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.Arguments = "view";
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
StreamReader sr = new StreamReader(proc.StandardOutput.BaseStream);
string line = "";
List<string> names = new List<string>();
while ((line = sr.ReadLine()) != null)
{
if (line.StartsWith(@"\\"))
names.Add(line.Substring(2).Split(' ')[0].TrimEnd());
}
sr.Close();
proc.WaitForExit();
lvSendTo.Items.Clear();
foreach (string name in names)
{ 
//Adding different computers to the list view.
lvSendTo.Items.Add(name);
}

When you click the send button, the following code will execute which will send the message to the selected computers:

foreach (ListViewItem item in lvSendTo.CheckedItems)
{
ListViewItem.ListViewSubItem subItem = item.SubItems.Add("Sending...."); 
Application.DoEvents();
this.Refresh();
try
{ 
// Prepare the sending message text
string sArguments = "send " + item.Text + " " + txtMessage.Text; 
Process proc = new Process(); 
proc.StartInfo.FileName = "net.exe"; 
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.Arguments = sArguments;
proc.StartInfo.UseShellExecute = false;
proc.Start(); 
proc.WaitForExit();
if (proc.ExitCode == 0) //Checks whether the process is completed successfully.
{
string sSource= "SendMessage";
string sLog="Application";
string sEvent = "Sent message to: " + item.Text + Convert.ToChar(13) + txtMessage.Text;
if (!EventLog.SourceExists(sSource))
{
EventLog.CreateEventSource(sSource, sLog);
}
EventLog.WriteEntry(sSource, sEvent);  //Writes the log to event log.
subItem.Text = "Message successfully sent!";
Application.DoEvents();
}
else
{ 
subItem.Text = "Failed to sent the message!";
Application.DoEvents();
}
}
catch
{
subItem.Text = "Failed to sent the message!";
Application.DoEvents();
continue;
} 
}

When executed, the following screen will be displayed:

Send.JPG

If you click on the View log button, a screen with sent and received messages will appear as follows:

View_log.JPG

The above form consists of a DataGridView (dgvLog), 2 checkboxes (chbReceived & chbSent) and refresh button. When loading the form or when the checkboxes are clicked or when a button is clicked, it will execute the function BindGrid() which reads the event log and extracts the required data into a datatable and this datatable is bound to the DataGridView control. The code for BindGrid() function is as follows:

private void BindGrid()
{
DataTable dtLog = new DataTable();
dtLog.Columns.Add("SendReceived", typeof(string));
dtLog.Columns.Add("From", typeof(string));
dtLog.Columns.Add("To", typeof(string)); 
dtLog.Columns.Add("Message", typeof(string)); 
dtLog.Columns.Add("Date", typeof(DateTime));
if (chbReceived.Checked)
{
EventLog log = new EventLog("System");
//Getting received messages from the event log.
foreach (EventLogEntry entry in log.Entries) 
{
string msg = entry.Message;
if (entry.Source.Equals("Application Popup") && msg.Contains
				("Messenger Service : Message from"))
{
DataRow dr = dtLog.NewRow();
int startIdx = msg.IndexOf("Messenger Service : Message from") +
"Messenger Service : Message from".Length + 1;
int length = msg.IndexOf(" ", startIdx) - startIdx;
dr["From"] = msg.Substring(startIdx, length);
dr["To"] = System.Environment.MachineName;
dr["Message"] = msg.Substring(msg.IndexOf(Convert.ToChar(13))).Trim();
dr["Date"] = entry.TimeGenerated;
dr["SendReceived"] = "Received";
dtLog.Rows.Add(dr);
}
}
}
if (chbSent.Checked)
{
EventLog log = new EventLog("Application");
foreach (EventLogEntry entry in log.Entries)//Getting sent messages from the event log.
{
string msg = entry.Message;
if (entry.Source.Equals("SendMessage"))
{
DataRow dr = dtLog.NewRow();
int startIdx = msg.IndexOf("Sent message to:") + "Sent message to:".Length + 1;
int length = msg.IndexOf(Convert.ToChar(13), startIdx) - startIdx;
dr["From"] = System.Environment.MachineName;
dr["To"] = msg.Substring(startIdx, length);
dr["Message"] = msg.Substring(msg.IndexOf(Convert.ToChar(13))).Trim();
dr["Date"] = entry.TimeGenerated;
dr["SendReceived"] = "Sent";
dtLog.Rows.Add(dr);
}
}
}
dtLog.DefaultView.Sort = "Date Desc";
dgvLog.DataSource = dtLog.DefaultView; 
}

Hope you have enjoyed this tool.

History

  • 28th October, 2008: Initial post