Thursday, December 17, 2009

Split String

string str = ds.Tables[0].Rows[0].ItemArray[1].ToString();
string[] str1 = str.Split(new char[] { ',' });
v1 = str1[0].ToString();
v2 = str1[1].ToString();

Tuesday, December 15, 2009

Get the IP address in a Windows application

Using .NET 1.1 and 2.0 to retrieve the IP address
Well, in .NET 1.1 and 2.0 there are methods in the System.Net namespace that do that. Speaking of System.Net, don't forget to include the using statement, to avoid the long lines I have below:

using System.Net;

// Get the hostname
string myHost = System.Net.Dns.GetHostName();
// Show the hostname
MessageBox.Show(myHost);
// Get the IP from the host name
string myIP = System.Net.Dns.GetHostByName(myHost).AddressList[0].ToString();
// Show the IP
MessageBox.Show(myIP);

Friday, December 11, 2009

How to send an Email through Window Application with C#

private void btnSend_Click(object sender, EventArgs e)
{
string sTo = txtToEmailAddress.Text;
string sFrom = txtEmail.Text;
string sSubject = txtSubject.Text;
string sBody = txtBody.Text;

try
{
MailMessage mail = new MailMessage(sFrom, sTo, sSubject, sBody);
SmtpClient client = new SmtpClient("IP address");
client.Send(mail);
MessageBox.Show("Mail Successfully Sent");
txtSubject.Text = "";
txtEmail.Text = "";
txtBody.Text = "";
}
catch (Exception)
{
MessageBox.Show("Mail Sending Fail");
}
}