Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.16 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Net;
  5.  
  6. namespace CMD_Tchat
  7. {
  8.     class Program
  9.     {
  10.         private string localDirectory;
  11.         private string filePath;
  12.  
  13.         static void Main(string[] args)
  14.         {
  15.             var p = new Program();
  16.             p.localDirectory = @"C:\ProgramData\cmd_tchat\";
  17.             string date = (DateTime.Now.Day + "." + DateTime.Now.Month + "." + DateTime.Now.Year);
  18.  
  19.             try
  20.             {
  21.                 if (!File.Exists(p.localDirectory + date + ".txt"))
  22.                 {
  23.                     File.Create(p.localDirectory + date + ".txt");
  24.                     return;
  25.                 } else
  26.                 {
  27.                     p.filePath = (p.localDirectory + date + ".txt");
  28.                 }
  29.             } catch (DirectoryNotFoundException)
  30.             {
  31.                 Directory.CreateDirectory(@"C:\ProgramData\cmd_tchat\");
  32.                 return;
  33.             }
  34.  
  35.             string ftpHost;
  36.             string ftpFullPath;
  37.             string user;
  38.             string password;
  39.  
  40.             try
  41.             {
  42.                 ftpHost = File.ReadAllLines(p.localDirectory + "ftp_settings")[2];
  43.                 ftpFullPath = "ftp://" + ftpHost + "/" + date + ".txt";
  44.                 user = File.ReadAllLines(p.localDirectory + "ftp_settings")[3];
  45.                 password = File.ReadAllLines(p.localDirectory + "ftp_settings")[4];
  46.  
  47.                 if (user == "-")
  48.                     user = "";
  49.                 else if (password == "-")
  50.                     password = "";
  51.  
  52.             } catch (FileNotFoundException)
  53.             {
  54.                 using (StreamWriter writer = new StreamWriter(p.localDirectory + "ftp_settings", true))
  55.                 {
  56.                     writer.WriteLine("# Line 3: IP Adress ; Line 4: FTP Username ; Line 5: FTP Password");
  57.                     writer.WriteLine("# Put a '-' if the value is null");
  58.                     writer.WriteLine("127.0.0.1");
  59.                     writer.WriteLine("spylx");
  60.                     writer.WriteLine("-");
  61.                     writer.Close();
  62.                 }
  63.                 return;
  64.             } catch (IndexOutOfRangeException)
  65.             {
  66.                 Console.WriteLine("Une valeur est vide dans " + p.localDirectory + "ftp_settings");
  67.                 Console.ReadKey();
  68.                 Environment.Exit(0);
  69.                 return;
  70.             }
  71.  
  72.             using (WebClient request = new WebClient())
  73.             {
  74.                 request.Credentials = new NetworkCredential(user, password);
  75.                 Console.WriteLine(user + password);
  76.                 byte[] fileData;
  77.                 try
  78.                 {
  79.                     fileData = request.DownloadData(ftpFullPath);
  80.                 } catch (WebException)
  81.                 {
  82.                     request.UploadFile(ftpFullPath, p.filePath);
  83.                     return;
  84.                 }
  85.  
  86.                 using (FileStream file = File.Create(p.filePath))
  87.                 {
  88.                     file.Write(fileData, 0, fileData.Length);
  89.                     file.Close();
  90.                 }
  91.             }
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement