Click here to Skip to main content
Licence CPOL
First Posted 28 Oct 2008
Views 26,792
Downloads 976
Bookmarked 53 times

Sending Messages to Multiple Computers in the Workgroup/Network

By | 28 Oct 2008 | Article
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

subashss

Architect
Hollitech International
India India

Member

Working on microsoft technologies for the past 9 years.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionWindows 7 compatibility PinmemberNoel Villaluna15:06 7 Sep '11  
QuestionI need Source code PinmemberRAHUL238523:21 2 Jan '11  
Generalno source code PinmemberNMRKMAN1:03 28 Sep '10  
QuestionIs the arguments in startinfo.arguemts to antoehr application is SAFE and SECURED?? PinmemberCoolCoder_New1:09 3 Feb '09  
QuestionNET SEND is disabled by default in XP SP2 or higher Pinmemberav3ng3r8522:56 22 Nov '08  
GeneralMessenger Service Disabled by default PinmemberIrwan Hassan16:18 3 Nov '08  
GeneralRe: Messenger Service Disabled by default Pinmembersubashss23:47 3 Nov '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 29 Oct 2008
Article Copyright 2008 by subashss
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid