RSS

Category Archives: C#

Network Monitor in C#

Ever thought about checking the internet uploading/downloading speed via C#? Well this network monitor can show you how!

Include using System.Net.NetworkInformation; in the .cs file
Drag 5 Labels onto the form.
Drag a timer and change the Enabled property to True and the Interval to 1000.
Intialize the following global variables:

NetworkInterface networkInterface;
long lngBytesSend = 0;
long lngBtyesReceived = 0;

Write the following code in the constructor:

foreach (NetworkInterface currentNetworkInterface in NetworkInterface.GetAllNetworkInterfaces())
if (currentNetworkInterface.Name.ToLower() == “local area connection”)
{
networkInterface = currentNetworkInterface;
break;
}
ShowNetworkInterfaceTraffice();

In the timer’s tick event call the function for showing the network traffic “ShowNetworkInterfaceTraffice()“.

Write the following code in the ShowNetworkInterfaceTraffice() function:

private void ShowNetworkInterfaceTraffice()
{
IPv4InterfaceStatistics interfaceStatistic = networkInterface.GetIPv4Statistics();
double bytesSentSpeed = (double)(interfaceStatistic.BytesSent – lngBytesSend) / 1024;
double bytesReceivedSpeed = (double)(interfaceStatistic.BytesReceived – lngBtyesReceived) / 1024;
labelSpeed.Text = (networkInterface.Speed / 1000000).ToString() + ” Mbps”;
labelReceived.Text = interfaceStatistic.UnicastPacketsReceived.ToString();
labelSent.Text = interfaceStatistic.UnicastPacketsSent.ToString();
labelUploading.Text = bytesSentSpeed.ToString() + ” KB/s”;
labelDownloading.Text = bytesReceivedSpeed.ToString() + ” KB/s “;
lngBytesSend = interfaceStatistic.BytesSent;
lngBtyesReceived = interfaceStatistic.BytesReceived;
}

You can download the project from the Box on the right!

 
Leave a comment

Posted by on June 29, 2011 in C#

 

Sending Emails Through C#

Here’s how you can develop your own Email sender in C# Windows Application with the inbuilt SMTP protocol using System.Net.Mail


3 Prerequisites:
  • Search for your Email Service Provider’s SMTP Server Address.
  • The Server Port Number
  • Configure your account settings to enable POP service so that the application is able to send mails via your account.

(POP is for outgoing mails and IMAP is for incoming mails. Since we are going to create an email sender and not an email receiver we need only POP enabled.)


Here we wil take the example of GMAIL,
  • Server Address: smtp.gmail.com
  • Server Port: 587
  • For account configuration login to Gmail, goto Settings, goto Forwarding and POP/IMAP, and Check on “Enable POP for all mail” and save the settings.




1. Now create a new windows application
2. Drag and drop the following controls from the toolbar:
  • 7 Text Boxes
  • 1 Rich Text Box
  • 2 Buttons
  • 8 Labels

3. Text boxes are renamed as:
  • textBoxId
  • textBoxPassword
  • textBoxSendTo
  • textBoxSubject
  • textBoxAttachment
  • textBoxAddr
  • textBoxPort

4. Buttons are renamed as:
  • buttonBrowse
  • buttonSend

5. Rich Text Box is renamed as richTextBoxMessage.
6. Provide a Password Char “*” (or any other) for the textBoxPassword.

7. Move the controls to give your form the following look:



8. Include the following line on the top of the .cs file:
using System.Net.Mail

9. At the buttonBrowse click event place the following code:
openFileDialog1.ShowDialog();
textBoxAttachment.Text = openFileDialog1.FileName;


This will open up file browser dialogue for selecting an attachment, and the location of selected file is put into the textbox.

10. At the buttonSend click event place the following code:

try{
SmtpClient client = new SmtpClient(textBoxAddr.Text);
MailMessage message = new MailMessage(textBoxId.Text, textBoxSendTo.Text);
message.Body = richTextBoxMessage.Text;
message.Subject = textBoxSubject.Text;
if (textBoxAttachment.Text != “”)
{ message.Attachments.Add(new Attachment(textBoxAttachment.Text)); }
client.Credentials = new System.Net.NetworkCredential(textBoxId.Text, textBoxPassword.Text);
client.Port = Convert.ToInt32(textBoxPort.Text);
client.Send(message);
MessageBox.Show(“Message delivered successfully!!!”, “ashKAP Mail Sender”, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
catch (Exception ex)
{
MessageBox.Show(“Message could not be sent.\nThe following error was returned:\n'” + ex.Message + “‘”, “ashKAP Mail Sender”, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}



Run the project and start mailing!!!!!!!!
Download the ashKAP Mail Sender Project from the Box on the right side.
(The project is made in Visual Studio 2010 and will not open in lower versions)

Here’s the software I made “ashKAP Prism”

 
Leave a comment

Posted by on March 29, 2011 in C#

 

Sending SMS via C# using Way2Sms

Create a new windows application.
Drag and drop 3 text boxes, 1 rich text box, 4 label, 1 button onto the form.
Rename the textboxes as following:
  • textbox1—>textBoxId
  • textbox2—>textBoxPassword
  • textbox3—>textBoxSendTo
  • richTextbox1—>richTextBoxText
  • button1—->buttonSend

In the solution explorer right cick on the Reference and add the following reference to the project:




Include the following lines:
using System;
using SMSClientLib;
using System.Net;


Double click on the button and add the following snippet to the click event:
string status = “”;
CookieContainer cookie = Login.Connect(textBoxId.Text, textBoxPassword.Text, out status);
string[] siteParameters = Login.GetSiteParameters(cookie);
string messgeSentResult = SendSMS.Send_Processing(textBoxSendTo.Text, richTextBoxText.Text, cookie, siteParameters);

 

Here’s the ashKAP Texter:

 
11 Comments

Posted by on March 26, 2011 in C#

 

C Sharp: How to make your own Battery Monitor

My laptop’s battery is totally screwed, it gets shut down within a minute once the power is off… often when i’m working the charger plug gets disturbed and come out a bit and the power is cut off and I don’t even get to know!

Unfortunately the computer suddenly shuts down while I’m working and all my data and progress is lost 😦

I thus tried to make my own Battery Monitor to tackle this problem. Now a popup window appears as soon as my laptop comes on battery!!!

Here’s the way… try out making one for your PC too…



C# Battery Monitor:
1). Create a new C# windows application project in Visual Studio.
2). Resize the form to 500×280 px
3). From the properties box make the values of MaximizeBox and MinimizeBox equal to false.
4). Drag the following controls from the toolbox(press Ctrl+Alt+X to show toolbox):
a). 1 Button
b). 1 Check Box
c). 1 Timer
d). 1 Progress Bar
e). 5 Labels
g). 1 Group Box
h). 2 Picture Box
5). Apply two different images to your picture box one showing a charging battery and another a discharging battery.
6). Rename the controls as follows:
label1—>BatteryStatus
label2—>ChargeRemaining
label3—>labelAlert
progressBar1—>BatteryIndicator
button1—>buttonOK
checkBox1—>chkRun
pictureBox1—>pictureBoxBattery
pictureBox2—>pictureBoxCharging

7). Arrange all the controls to give your form the following look:

8). Note the the 2 picture boxes overlap each other.
9). Write the following code:
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Media;

namespace Battery
{
public partial class Battery : Form
{
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey(“SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run”, true);
PowerStatus power = SystemInformation.PowerStatus;
int x,y;
public Battery()
{
InitializeComponent();
if (rkApp.GetValue(“MyApp”) == null)
{chkRun.Checked = false;}
else
{chkRun.Checked = true;}
float secondsRemaining = power.BatteryLifePercent;
if (secondsRemaining >= 0)
{
ChargeRemaining.Text = (secondsRemaining * 100).ToString() + “% available.”;
}
else
{
ChargeRemaining.Text = string.Empty;
}
}

private void Battery_Load(object sender, EventArgs e)
{
RefreshStatus();
timer1.Enabled = true;
y = this.Left;
}

private void RefreshStatus()
{
int powerPercent = (int)(power.BatteryLifePercent * 100);
if (powerPercent <= 100) BatteryIndicator.Value = powerPercent; else BatteryIndicator.Value = 0; switch (power.PowerLineStatus)
{
case PowerLineStatus.Online: ;
groupBox1.Text = “Running On Mains”;
x = 1;
labelAlert.ForeColor = Color.Chartreuse;
labelAlert.Text=”Plugged In”;
pictureBoxCharging.Visible = true;
pictureBoxBattery.Visible = false;
float secondsRemaining = power.BatteryLifePercent;
if (secondsRemaining >= 0)
{
ChargeRemaining.Text = (secondsRemaining * 100).ToString() + “% available.”;
}
else
{
ChargeRemaining.Text = string.Empty;
}
BatteryStatus.Text = power.BatteryChargeStatus.ToString();
break;

case PowerLineStatus.Offline:


groupBox1.Text = “Running On Battery”;
if (x == 1)
{
SoundPlayer simpleSound = new SoundPlayer(@”c:\Windows\Media\Windows Exclamation.wav”);
simpleSound.Play();
labelAlert.ForeColor = Color.Red;
labelAlert.Text = “On Battery”;
pictureBoxCharging.Visible = false;
pictureBoxBattery.Visible = true;
this.WindowState = FormWindowState.Normal;
this.Left = y;
BatteryStatus.Text = power.BatteryChargeStatus.ToString();
}
x = 0;
break;

case PowerLineStatus.Unknown:

break;
}

}

private void timer1_Tick(object sender, EventArgs e)
{
RefreshStatus();
}

private void buttonOK_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}

private void chkRun_CheckedChanged(object sender, EventArgs e)
{
if (chkRun.Checked)
{
rkApp.SetValue(“MyApp”, Application.ExecutablePath.ToString());
}
else
{
rkApp.DeleteValue(“MyApp”, false);
}
}
}
}

 

Run your application! 🙂
While charging:

While running on battery:


 
Leave a comment

Posted by on March 8, 2011 in C#

 

C# Web Browser with Popup Blocker

C# Web Browser with pop-up blocker:
1). Create a new C# windows application project in Visual Studio.
2). Resize the form to 800×500 px.
3). Drag the following controls from the toolbox(press Ctrl+Alt+X to show toolbox):
a). 1 Buttons (name it as buttonGo)
b). 1 Text Boxes (name it as buttonGo)
c). 1 Microsoft Web Browser
NOTE: We’ll use the activeX web browser control and not the web browser that is directly visible in the toolbox. To get this active web browser control right click on toolbox, select “Choose Toobox Items”, goto COM Components tab, select Microsoft Web Browser and click OK.

5). Arrange all the controls to give your form the following look:

6). Button “Go” is for navigating to the address in the text box on the left to it.
7). Replace “Afact” everywhere in the code below with the name you gave to your application.

8). Write the following code to buttonGo events and NewWindow2 event of the web browser control:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Afact
{
public partial class Afact : Form
{

public Afact()
{
InitializeComponent();
}

private void Afact_Load(object sender, EventArgs e)
{
axWebBrowser1.Navigate(“www.google.com”);

}

private void buttonGo_Click(object sender, EventArgs e)
{
axWebBrowser1.Navigate(textBoxURL.Text);
}

private void axWebBrowser1_NewWindow2(object sender, AxSHDocVw.DWebBrowserEvents2_NewWindow2Event e)
{
// Is document ready state complete ? If not, that’s an ad popup. Don’t allow it
if (axWebBrowser1.ReadyState != SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
{
e.ppDisp = null;
e.cancel = true;
return;
}

/* Else let’s create a new form instance and
send the new web browser instance back to the caller.*/
Afact newwindow = new Afact();
newwindow.Text = “Afact New Window”;
e.ppDisp = newwindow.axWebBrowser1.Application;
newwindow.Show();
}

}
}

 

Download the project from the BOX on the right side!!!

 

 
Leave a comment

Posted by on March 7, 2011 in C#

 

Creating a C# Process Blocker(Virus)

The code below is used to block a certain process in your computer. Whenever you start an application that is blocked by process blocker it will not run at any cost… Indirectly its a sort of a virus which can ruin your computer OR YOUR FRIEND’S 🙂

Let’s get this started…

1). Create a new C# windows application in Visual Studio.
2). Drag from the ToolBox onto the window the following items:
a). 1 listbox
b). 2 buttons
c). 1 label
d). 1 timer
e). 1 label
3). Rename the following from their properties box:
listbox1—>listboxProcess
textBox1—>textBoxName
button1—>buttonBlock
button2—>buttonAllow
label1—>labelStatus
4). Also, make the value of HorizontalScrollBar in the properties of listbox as “true”.
5). Value of Enabled=true and Interval=100 for the timer in the properties box.
6). Give your form the following look:

7). Place the labelStatus below the buttons. This will tell us if the application is blocking a process or not.
8). Include the following line in the .cs file: “using System.Diagnostics;” This will not be automatically written by      Visual Studio.
9). Write the following lines of code in form’s load event, button click events, etc etc.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;//INCLUDED MANUALLY

namespace TaskMan
{
public partial class FormTaskMan : Form
{
string targetProcess;
public FormTaskMan()
{
InitializeComponent();
}

private void FormTaskMan_Load(object sender, EventArgs e)
{
//GETTING THE LIST OF PROCESSES
foreach (Process p in Process.GetProcesses())
{
listBoxProcess.Items.Add(p.ProcessName+”—–>”+p.MainWindowTitle);

}
labelStatus.Text = “”;
}

private void buttonBlock_Click(object sender, EventArgs e)
{
targetProcess = textBoxName.Text;
labelStatus.Text = textBoxName.Text + ” BLOCKED!”;
}

private void buttonAllow_Click(object sender, EventArgs e)
{
targetProcess = “”;
labelStatus.Text =”ALLOWED!”;
}

private void timer1_Tick(object sender, EventArgs e)
{
foreach (Process p1 in Process.GetProcesses())
{
//targetProcess WILL BE BLOCKED, WINDOWS TASKMANAGER WILL BE BLOCKED, AND THE APPLICATION ITSELF CANNOT BE BLOCKED!
if ((p1.ProcessName==targetProcess) || p1.ProcessName.StartsWith(“taskmgr”) && p1.ProcessName.StartsWith(“TaskMan”)==false)
{
try
{
p1.Kill();
}
catch (Win32Exception)
{
//Process cannot be blocked 😦
}
}
}
}

}
}

Enjoy… 😛
Try it our with a few processes like “wmplayer” for Windows Media Player, “firefox” for Mozilla Firefox, “iexplore” for Internet Explorer…

Download the project from the BOX on the right side!!!

 
Leave a comment

Posted by on March 7, 2011 in C#

 

C# Web Browser

Web Browser was the first project I made in C#.NET. It was the the easiest best thing I could do!
So I decided this to be the first post on myjuet. Hope you’ll like it

C# Web Browser:

1). Create a new C# windows application project in Visual Studio.
2). Resize the form to 800×500 px. And rename it as FormBrowser.
3). Drag the following controls from the toolbox(press Ctrl+Alt+X to show toolbox):
a). 13 Buttons
b). 2 Text Boxes
c). 1 Progress Bar
d). 1 Web Browser Control
e). 4 Labels
4.) Rename the controls as follows:
button1—>buttonGo
button2—>buttonBack
button3—>buttonForward
button4—>buttonCancel
button5—>buttonRefresh
button6—>buttonHome
button7—>buttonHtml
button8—>buttonPrint
button9—>buttonPrintSettings
button10—>buttonSave
button11—>buttonSettings
button12—>buttonWeb
button13—>buttonImageSearch
textBox1—>textBoxURL
textBox1—>textBoxSearch
label1—>labelStatus

Don’t rename any other control.

5). Arrange all the controls to give your form the following look:

6). “<“, “>” stand for Go Back & Go Forward.
7). “Go” is for navigating to the address in the text box on the left to it.
8). “X” for stop loading, “Refresh” for refreshing the page, “Home” for navigating to the Home Page, etc etc.
9). Write the following code to button-click events and other functions:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ashKAP_Web_Browser /* DO NOT OVERWRITE YOUR PROJECT’S NAMESPACE STATEMENT WITH THIS STATEMENT IN CASE YOU COPY THIS PROGRAM */
{
public partial class FormBrowser : Form
{
string url;
public FormBrowser()
{
InitializeComponent();

}
private void FormBrowser_Load(object sender, EventArgs e)
{
webBrowser1.ScriptErrorsSuppressed = true;
progressBar1.Value = 0;
webBrowser1.Navigate(“www.google.com”);
}

private void buttonGo_Click(object sender, EventArgs e)
{
url = textBoxURL.Text;
if (!webBrowser1.IsOffline)
{
progressBar1.Value = 0;
webBrowser1.Navigate(url);
}
else
{
MessageBox.Show(“OOPS!!! ashKAP Browser’s not connected to internet X-(“, “ashKAP Browser’s OFFLINE”, MessageBoxButtons.OK, MessageBoxIcon.Error);
labelStatus.Text = “OFFLINE”;
}

}

private void buttonCancel_Click(object sender, EventArgs e)
{
progressBar1.Value = 0;
webBrowser1.Stop();
}

private void buttonRefresh_Click(object sender, EventArgs e)
{
textBoxURL.Text = “”;
progressBar1.Value = 0;
webBrowser1.Refresh();
}

private void buttonHome_Click(object sender, EventArgs e)
{
textBoxURL.Text = “”;
progressBar1.Value = 0;
webBrowser1.GoHome();
}

private void buttonForward_Click(object sender, EventArgs e)
{
if (webBrowser1.CanGoForward == true)
{
textBoxURL.Text = “”;
progressBar1.Value = 0;
webBrowser1.GoForward();
}

}

private void buttonBack_Click(object sender, EventArgs e)
{
if (webBrowser1.CanGoBack == true)
{
textBoxURL.Text = “”;
progressBar1.Value = 0;
webBrowser1.GoBack();
}
}

private void buttonHtml_Click(object sender, EventArgs e)
{
MessageBox.Show(webBrowser1.DocumentText, “HTML Code of the page”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void buttonSettings_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(“inetcpl.cpl”);
}

private void buttonPrint_Click(object sender, EventArgs e)
{
webBrowser1.Print();
webBrowser1.ShowPrintPreviewDialog();
}

private void buttonPrintSetup_Click(object sender, EventArgs e)
{
printDialog1.ShowDialog();
}

private void buttonWebSearch_Click(object sender, EventArgs e)
{
string sss = textBoxSearch.Text.Replace(‘ ‘, ‘+’);
webBrowser1.Navigate(“www.google.com/images?q=” + sss);
}

private void buttonSave_Click(object sender, EventArgs e)
{
webBrowser1.ShowSaveAsDialog();
}

private void buttonImageSearch_Click(object sender, EventArgs e)
{
string sss = textBoxSearch.Text.Replace(‘ ‘, ‘+’);
webBrowser1.Navigate(“www.google.com/images?q=” + sss);
}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
progressBar1.Value = 100;
}


private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
progressBar1.Value += 10;
}

private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
labelStatus.Text = “Done”;
}

//ADDED EXPLICITLY IN THE DESIGNER.CS FILE
void webBrowser1_StatusTextChanged(object sender, System.EventArgs e)
{
labelStatus.Text = webBrowser1.StatusText;

}
}
}

 

Download the project from the BOX on the right side!!!

 

 
Leave a comment

Posted by on March 6, 2011 in C#