Categories
Search

Ping the remote system, copy file to the remote system

Below code help you to Ping a remote system to check its status, and copy any file to that remote system.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.IO;
 
namespace Examples.System.Net.NetworkInformation.PingTest
{
    public class PingExample
    {
        // args[0] can be an IPaddress or host name.
        public static void Main (string[] args)
        {
            try
	{
	Ping pingSender = new Ping ();
             PingOptions options = new PingOptions ();
 
            // Use the default Ttl value which is 128,
            // but change the fragmentation behavior.
            options.DontFragment = true;
 
            // Create a buffer of 32 bytes of data to be transmitted.
            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            byte[] buffer = Encoding.ASCII.GetBytes (data);
            int timeout = 120;
            PingReply reply = pingSender.Send (args[0], timeout, buffer, options);
            if (reply.Status == IPStatus.Success)
            {
                Console.WriteLine ("Address: {0}", reply.Address.ToString ());
                Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime);
                Console.WriteLine ("Time to live: {0}", reply.Options.Ttl);
                Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment);
                Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length);
	   try
	   {
	          string strHostComp = args[0].ToString();
	          string strHostFilePath = @"\\" + strHostComp + @"\C$\perforce.txt";
		if (File.Exists(strHostFilePath))
		{
		File.Delete(strHostFilePath);
		Console.WriteLine ("Previous version of the File is deleted Successfuly.");
		}
		File.Copy(@"C:\perforce.txt",strHostFilePath);
		Console.WriteLine ("File is Copied Successfuly.");
		Console.ReadLine();
		}
	catch(System.IO.FileLoadException ex)   { Console.WriteLine ("File is locked by another process. Cannot be replaced.");	}
	catch(Exception ex)
	{   Console.WriteLine ("File is Not Copied: " + ex.Source + ex.Message + ex.StackTrace);  }
            }
	else {	Console.WriteLine ("Ping Cannot be reached");	}
	}
	catch(Exception ex) { Console.WriteLine ("Ping not reached: " + ex.Message); }
        }
    }
}

Tags: , , , , , , , , , , , ,

Leave a Reply