Guest User

ConsoleDownloader

a guest
Sep 22nd, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Net;
  6.  
  7. namespace TestDownload
  8. {
  9.     class Program
  10.     {
  11.         static string fileInProgress = string.Empty;
  12.         static void Main(string[] args)
  13.         {
  14.             Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
  15.  
  16.             List<string> fileurls = new List<string>();
  17.             string[] files = Directory.GetFiles(@"E:\Edu\Books\Books");
  18.             int count = 0;
  19.             foreach (string filename in files)
  20.             {
  21.                 FileInfo fn = new FileInfo(filename);
  22.                 count++;
  23.                 fileurls.Add("http://192.168.2.4/" + fn.Name);
  24.             }
  25.  
  26.             foreach (string url in fileurls)
  27.             {
  28.                 fileInProgress = url.Substring(url.LastIndexOf("/") + 1, (url.Length - url.LastIndexOf("/") - 1));
  29.                 DownloadFile(url, fileInProgress);
  30.             }
  31.         }
  32.  
  33.         static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
  34.         {
  35.             try
  36.             {
  37.                 Console.ForegroundColor = ConsoleColor.Yellow;
  38.                 Console.WriteLine("Program interrupted..deleting corrupted file");
  39.                 Console.ResetColor();
  40.                 if (File.Exists(fileInProgress))
  41.                 {
  42.                     while (IsFileLocked(fileInProgress))
  43.                     {
  44.                         System.Threading.Thread.Sleep(1000);
  45.                     }
  46.                     File.Delete(fileInProgress);
  47.                 }
  48.             }
  49.             catch
  50.             {
  51.                 Console.WriteLine("Error occured.");
  52.             }
  53.         }
  54.  
  55.         private static bool IsFileLocked(string fileName_in)
  56.         {
  57.             FileInfo inpFileInfo = new FileInfo(fileName_in);
  58.             FileStream stream = null;
  59.             try
  60.             {
  61.                 stream = inpFileInfo.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
  62.             }
  63.             catch (IOException)
  64.             {
  65.                 //the file is unavailable because it is:
  66.                 //still being written to
  67.                 //or being processed by another thread
  68.                 //or does not exist (has already been processed)
  69.                 return true;
  70.             }
  71.             finally
  72.             {
  73.                 if (stream != null)
  74.                     stream.Close();
  75.             }
  76.  
  77.             //file is not locked
  78.             return false;
  79.         }
  80.  
  81.         private static void DownloadFile(string url, string fileInProgress)
  82.         {
  83.             try
  84.             {
  85.                 Console.WriteLine("Downloading " + fileInProgress);
  86.                 WebClient wb = new WebClient();
  87.                 wb.DownloadFile(new Uri(url), fileInProgress);
  88.             }
  89.             catch (WebException webexp)
  90.             {
  91.                 if (File.Exists(fileInProgress))
  92.                 {
  93.                     File.Delete(fileInProgress);
  94.                 }
  95.                 Console.WriteLine("Error occured while downloading " + fileInProgress + "\n Msg -" + webexp.ToString());
  96.             }
  97.             catch (NotSupportedException nsExp)
  98.             {
  99.                 if (File.Exists(fileInProgress))
  100.                 {
  101.                     File.Delete(fileInProgress);
  102.                 }
  103.                 Console.WriteLine("Error occured while downloading " + fileInProgress + "\n Msg -" + nsExp.ToString());
  104.             }
  105.         }
  106.     }
  107. }
Add Comment
Please, Sign In to add comment