Search This Blog

Showing posts with label Nework Programming. Show all posts
Showing posts with label Nework Programming. Show all posts

Saturday, March 15, 2014

How to Detect Network Availability and Address Changes in a network

Detect Network Availability and Address Change in Network

We can use the System.Net.NetworkInformation Namespace provides access to network traffic data,network address information,and notification of address changes for the local computer method to all adapters available in the network. System.Net.NetworkInformation also contains the ping utility information.The ping utility is used to check weather a computer is available across the network.

Here is code given to check the network availability ;

using System;
using System.Net;
using System.Net.NetworkInformation;

namespace AddressChanges
{
    public class NetworkingDemo
    {
        public static void Main()
        {
            NetworkChange.NetworkAddressChanged += new 
             NetworkAddressChangedEventHandler(AddressChangedCallback);
            Console.WriteLine("Listening for address changes. Press any key to exit.");
            Console.ReadLine();
        }
        static void AddressChangedCallback(object sender, EventArgs e)
        {
            
            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            foreach(NetworkInterface n in adapters)
            {
                Console.WriteLine("   {0} is {1}", n.Name, n.OperationalStatus);
            }
        }
    }
}

Thursday, March 13, 2014

How to Upload And Download a File Using FTP in C# .net

To Upload a file on FTP design a windows form like below:


We can upload and download a file on FTP using System.Net. FtpWebRequest and System.Net. FtpWebResponse  class,
To upload we can call this property:
request.Method = WebRequestMethods.Ftp.UploadFile;

To Download we can call this property:
 request.Method = WebRequestMethods.Ftp.DownloadFile;


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.Net;
using System.IO;

namespace UploadDownloadFTP
{
    public partial class frmUpload : Form
    {
        public frmUpload()
        {
            InitializeComponent();
        }

        private void frmUpload_Load(object sender, EventArgs e)
        {

        }

        private void btnBrows_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtFilePath.Text = openFileDialog1.FileName;
            }
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.website.com/test.htm");
            request.Method = WebRequestMethods.Ftp.UploadFile;

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential("anonymous", "naveenranjan@gmail.com");

            // Copy the contents of the file to the request stream.
            StreamReader sourceStream = new StreamReader(txtFilePath.Text);
            byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
            sourceStream.Close();
            request.ContentLength = fileContents.Length;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(fileContents, 0, fileContents.Length);
            requestStream.Close();

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

           lblUploadStatus.Text= "Upload File Complete status: " + response.StatusDescription;

            response.Close();
        }

     
    }
}

To Download a File from FTP, Design a windows form as given below:





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.IO;
using System.Net;

namespace UploadDownloadFTP
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void btnBrows_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                // Get the object used to communicate with the server.
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.website.com/test.htm");
                request.Method = WebRequestMethods.Ftp.DownloadFile;

                // This example assumes the FTP site uses anonymous logon.
                request.Credentials = new NetworkCredential("anonymous", "naveenranjan@gmail.com");

                FtpWebResponse response = (FtpWebResponse)request.GetResponse();

                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream);
                Console.WriteLine(reader.ReadToEnd());

                lblDownloadStatus.Text = "Download Complete, status "+ response.StatusDescription;

                reader.Close();
                response.Close();
            }
        }
    }
}