Thursday, September 20, 2012

FTP Downloads in .NET

There are many situation in which you would want to connect to an FTP server and perform downloading of FTP Files. here is a helper class to perorm that.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace Yournamespace.Core.FTP
{
    public class FTPManager
    {
        private string _FTPHost;
        private string _FTPUser;
        private string _FTPPassword;

        public string FTPHost
        {
            get { return _FTPHost; }
            set { _FTPHost = value; }
        }

        public string FTPUser
        {
            get { return _FTPUser; }
            set { _FTPUser = value; }
        }

        public string FTPPassword
        {
            get { return _FTPPassword; }
            set { _FTPPassword = value; }
        }

        public void Upload(string filename)
        {

        }

        public void Download(string filename)
        {
            FileStream outputStream = new FileStream(filename, FileMode.Create);
            FtpWebRequest request = null;
            FtpWebResponse response = null;
            Stream ftpStream = null;

            try
            {
                request = (FtpWebRequest)FtpWebRequest.Create(new
                  Uri("ftp://" + this.FTPHost + "/" + filename));
                request.Method = WebRequestMethods.Ftp.DownloadFile;
                request.UseBinary = true;
                request.Credentials = new NetworkCredential(this.FTPUser, this.FTPPassword);
                response = (FtpWebResponse)request.GetResponse();
                ftpStream = response.GetResponseStream();
                long cl = response.ContentLength;
                int bufferSize = 2048;
                int readCount;
                byte[] buffer = new byte[bufferSize];

                readCount = ftpStream.Read(buffer, 0, bufferSize);
                while (readCount > 0)
                {
                    outputStream.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }

            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                ftpStream.Close();
                outputStream.Close();
                response.Close();
            }

        }
    }
}

Email Manager Class

There are many situations in which you would want to send emails from your application. This class just manages that.

see the code snippet below and please feel free to copy and implement in your application.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Yournamespace.Core.Emails
{
    public static class EmailManager
    {
        public static void SendEmail(string emailTo, string emailFrom, string emailSubject, string emailMessage)
        {
            List<System.Net.Mail.MailAddress> toList = new List<System.Net.Mail.MailAddress>();
            System.Net.Mail.MailAddress address = new System.Net.Mail.MailAddress(emailTo);

            toList.Add(address);
            SendEmail(toList, null, emailFrom, emailSubject, emailMessage, null);
        }

        public static void SendEmail(List<System.Net.Mail.MailAddress> list, string emailSubject, string emailMessage)
        {
            SendEmail(list, null, null, emailSubject, emailMessage, null);
        }

        public static void SendEmail(List<System.Net.Mail.MailAddress> toList, List<System.Net.Mail.MailAddress> ccList, string emailFrom, string emailSubject, string emailMessage, List<System.Net.Mail.Attachment> attachments)
        {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();

            try
            {
                //setup client
                client.Host = Configuration.ConfigManager.GetSMTPServer();

                //add all to email addresses.
                if (toList != null)
                {
                    foreach (System.Net.Mail.MailAddress mailAddrs in toList)
                    {
                        msg.To.Add(mailAddrs);
                    }
                }

                //add all cc email addresses.
                if (ccList != null)
                {
                    foreach (System.Net.Mail.MailAddress mailAddrs in ccList)
                    {
                        msg.CC.Add(mailAddrs);
                    }
                }

                if (emailFrom != null)
                {
                    msg.From = new System.Net.Mail.MailAddress(emailFrom);
                }
                else
                {
                    msg.From = new System.Net.Mail.MailAddress(Configuration.ConfigManager.GetDefaultFromAddress());
                }

                msg.Subject = emailSubject;
                msg.Body = emailMessage;

                //add attachments
                if (attachments != null)
                {
                    foreach (System.Net.Mail.Attachment attach in attachments)
                    {
                        msg.Attachments.Add(attach);
                    }
                }

                try
                {
                    client.Send(msg);
                }
                catch (Exception e)
                {
                    throw new Exception(EmailErrors.EmailError_SendEmailFailed.ToString() + " - " + (int)EmailErrors.EmailError_SendEmailFailed + " - " + e.Message);
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                client.Dispose();
                msg.Dispose();
            }
        }
    }
}


Wednesday, September 19, 2012

Using unfuddle with Subversion to create a free online project repository

Unfuddle is an online service that offers to maintain a free repository of source code for your project. At the time of writing this post the folllowing offer was valid "always free "Private" plan: (512MB, 1 Project, 2 Collaborators, 3 Notebook Pages, SSL)"

This online service also offers wiki pages and has a ticketing system which can be used to track work requests and issues.

Website
http://www.unfuddle.com/


Login Screen

Main Screen



Windows GUI GAC Viewer

For those people who work in .NET there is often a need to view the Global Assembly Cache and also a need to install or uninstall assemblies from the GAC. There is a tool just to do that and this tool is very convenient. Its called WinGAC.

Website
http://wingac.codeplex.com/

Screenshots

Sunday, September 16, 2012

Visual Studio 2012 Express

Its out and available for download.

Screenshots




Web Platform Installer 4.0

A new version of the web platform installer is out. version 4.0. This is a really convenient tool to install all web development Microsoft applications onto the development machine.

Download
http://www.microsoft.com/web/downloads/platform.aspx

Screenshots



Thursday, September 13, 2012

Webservice Testing Using SoapUI, LoadUI

SoapUI and LoadUI provide a really good platform for web-service testing and load testing and it really customizable and easy to use.

There are situations when a web service needs to be both tested for functionality as well as for performance. SoapUI provides the framework for testing the web services functionality wise and LoadUI provides the framework for stress testing. In this post i will go through the steps involved in setting up both soap and load UI tools to perform the necessary testing.

1. Creating and Setting up a new WebService Project in SoapUI.

Step 1. Create a new SoapUI Project

Step 2. Give the projetc a name and specify the initial wsdl for the project.

Step 3. Once the project is created the Soap elements should appear in the project as shown in the screenshot.

SQL Lite Database Browser

How do you lookup the data in an SQL Lite database. Well you download and install SQLLite Browser.... thats how....

Download
http://sqlitebrowser.sourceforge.net/

Screenshots