Showing posts with label Microsoft Dot Net. Show all posts
Showing posts with label Microsoft Dot Net. Show all posts

Tuesday, September 14, 2010

Copy Files from FTP Server to Local or any other File System Server

Have you ever tried or worked on FTP files and you want to Copy them to specified drive of a server and then make some operations to it..?
If No, and Now you want to do the same or want to keep it for the future then this post is best for you.
I would recomment you to add it to your favorites for Future reference.

For simplicity, I will use Local Computer as FTP Server.
Step1: Create a FTP Folder. go to My Compute  then C:\inetput\ftproot\ and then create a Folder which will be treated as FTP folder. Note: IIS should be installed.
Step2: Save any TEXT File let say  basefile.txt which has some text.
Step3: Open the Visual Studio 2005/8/10 add a Console application.
Step4: Add a .cs File and rename the file as FTPFileTransfer.cs
Step5 Add Following code.


        public class  FTPFileTransfer
        {



           public FtpStatusCode Download(string destinationFile, Uri downloadUri, string userName, string password)
           {
               try
               {
                   // Check if the URI is and FTP site
                   if (downloadUri.Scheme != Uri.UriSchemeFtp)
                   {
                       throw new ArgumentException("Invalid FTP site");
                   }
                   // Set up the request
                   FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(downloadUri);
                   // Credentials for FTP URI
                   ftpRequest.Credentials =
                   new NetworkCredential(userName, password);
                   // Set method as file download.
                   ftpRequest.Method =
                   WebRequestMethods.Ftp.DownloadFile;
                   // get the response object
                   FtpWebResponse  ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
                   Stream stream = null;
                   StreamReader reader = null;
                   StreamWriter writer = null;
                   // get the file as a stream from the response
                      object and write it as a file stream to the local disk
                   try
                   {
                       stream = ftpResponse.GetResponseStream();
                       reader = new StreamReader(stream, Encoding.UTF8);
                       writer = new StreamWriter(destinationFile, false);
                       writer.Write(reader.ReadToEnd());
                       return ftpResponse.StatusCode;
                   }
                   finally
                   {
                       // Close streams
                       stream.Close();
                       reader.Close();
                       writer.Close();
                   }
               }
               catch (Exception ex)
               {
                   throw ex;
               }
           }
        }


Now you can create the object of this class and Call Download Method.

Please put your comments about the article..

Monday, August 30, 2010

Putting Message in the MSMQ through the C#.net

Before I start with the Actual Code used to Write and Read a Message, I would like to give brief overview on the MSMQ

Microsoft Message Queuing or MSMQ is a Message Queue implementation developed by Microsoft and deployed in its Windows Server operating systems since Windows NT 4 and Windows 95. The latest Windows 7 also includes this component. In addition to its mainstream server platform support, MSMQ has been incorporated into Microsoft Embedded platforms since 1999 and the release of Windows CE 3.0.
MSMQ is essentially a messaging protocol that allows applications running on separate servers/processes to communicate in a failsafe manner. A queue is a temporary storage location from which messages can be sent and received reliably, as and when conditions permit. This enables communication across heterogeneous networks and between computers which may not always be connected. By contrast, sockets and other network protocols assume that direct connections always exist.


MSMQ has been available to developers on Microsoft platforms since 1997,[2] and has commonly been used in enterprise software built with Visual Studio, both in the native pre-.NET incarnation (version 5 and 6), and in Visual Studio .NET. Microsoft also has incorporated MSMQ in its messaging technology framework, Windows Communication Foundation (WCF). Under WCF, MSMQ can be used to provide secure, reliable transport with a unified programming model compatible with other communications standards.

MSMQ is responsible for reliably delivering messages between applications inside and outside the enterprise. MSMQ ensures reliable delivery by placing messages that fail to reach their intended destination in a queue and then resending them once the destination is reachable. It also supports security and priority based messaging. Dead letter queues can be created for looking at messages which timed out or failed for other reasons.

Following is the Code:
public class MSMQ_rw