Advertisement
Guest User

FTP Tchat by SpyLX

a guest
Jan 19th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Net;
  12.  
  13. namespace FTP_Tchat_by_SpyLX
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         private string localDirectory;
  18.         private string filePath;
  19.         private string fileNameToday;
  20.         private string ftpFullPath;
  21.         private string username;
  22.         private string password;
  23.         private string[] messages;
  24.         private short tryAgainOrError;
  25.         private long size;
  26.         private long sizeChange;
  27.         private WebClient request;
  28.         private FtpWebRequest requestDir;
  29.         private FtpWebResponse response;
  30.  
  31.         public Form1()
  32.         {
  33.             InitializeComponent();
  34.             localDirectory = @"C:\ProgramData\cmd_tchat\";
  35.         }
  36.  
  37.         private void Form1_Load(object sender, EventArgs e)
  38.         {
  39.             up:
  40.             string date = (DateTime.Now.Day + "." + DateTime.Now.Month + "." + DateTime.Now.Year);
  41.             fileNameToday = localDirectory + date + ".txt";
  42.  
  43.             // Vérification si le fichier d'aujourd'hui existe, sinon le créer
  44.             try
  45.             {
  46.                 if (!File.Exists(fileNameToday))
  47.                 {
  48.                     File.Create(fileNameToday).Close();
  49.                     goto up;
  50.                 }
  51.                 else
  52.                 {
  53.                     filePath = (fileNameToday);
  54.                 }
  55.             }
  56.             catch (DirectoryNotFoundException)
  57.             {
  58.                 Directory.CreateDirectory(localDirectory.Remove(localDirectory.Length - 1));
  59.                 goto up;
  60.             }
  61.  
  62.             string ftpHost;
  63.  
  64.             // Récupération des valeurs de ftp_settings
  65.             try
  66.             {
  67.                 ftpHost = File.ReadAllLines(localDirectory + "ftp_settings")[2];
  68.                 ftpFullPath = "ftp://" + ftpHost + "/" + date + ".txt";
  69.                 username = File.ReadAllLines(localDirectory + "ftp_settings")[3];
  70.                 password = File.ReadAllLines(localDirectory + "ftp_settings")[4];
  71.  
  72.                 if (username == "-")
  73.                     username = "";
  74.                 else if (password == "-")
  75.                     password = "";
  76.  
  77.             }
  78.             catch (FileNotFoundException)
  79.             {
  80.                 File.Create(localDirectory + "ftp_settings").Close();
  81.                 using (StreamWriter writer = new StreamWriter(localDirectory + "ftp_settings", true))
  82.                 {
  83.                     writer.WriteLine("# Line 3: IP Adress ; Line 4: FTP Username ; Line 5: FTP Password");
  84.                     writer.WriteLine("# Put a '-' if the value is null");
  85.                     writer.WriteLine("127.0.0.1");
  86.                     writer.WriteLine("spylx");
  87.                     writer.WriteLine("-");
  88.                     writer.Close();
  89.                 }
  90.                 goto up;
  91.             }
  92.             catch (IndexOutOfRangeException)
  93.             {
  94.                 MessageBox.Show("Une valeur est vide dans " + localDirectory + "ftp_settings");
  95.                 Application.Exit();
  96.                 goto up;
  97.             }
  98.  
  99.             // Téléchargement du fichier d'aujourd'hui
  100.             using (request = new WebClient())
  101.             {
  102.                 request.Credentials = new NetworkCredential(username, password);
  103.  
  104.                 TestSize();
  105.                 sizeChange = size;
  106.  
  107.                 byte[] fileData;
  108.  
  109.                 // Prendre le fichier d'aujourd'hui, si il n'existe pas, le créer sur le serveur FTP
  110.                 try
  111.                 {
  112.                     fileData = request.DownloadData(ftpFullPath);
  113.                 }
  114.                 catch (WebException)
  115.                 {
  116.                     try
  117.                     {
  118.                         request.UploadFile(ftpFullPath, filePath);
  119.                         goto up;
  120.                     }
  121.                     catch (WebException)
  122.                     {
  123.                         tryAgainOrError++;
  124.                         if (tryAgainOrError <= 100)
  125.                         {
  126.                             goto up;
  127.                         } else
  128.                         {
  129.                             MessageBox.Show("Impossible d'accéder au serveur.");
  130.                             Application.Exit();
  131.                         }
  132.                     }
  133.                     return;
  134.                 }
  135.  
  136.                 // Écriture du fichier téléchargé ci-dessus
  137.                 using (FileStream file = File.Create(filePath))
  138.                 {
  139.                     file.Write(fileData, 0, fileData.Length);
  140.                     file.Close();
  141.                 }
  142.             }
  143.  
  144.             // Ajout du texte du fichier d'aujourd'hui dans RichTextBox1
  145.             messages = File.ReadAllLines(filePath, Encoding.UTF8);
  146.             richTextBox1.Lines = messages;
  147.         }
  148.  
  149.         private void button1_Click(object sender, EventArgs e)
  150.         {
  151.             using (StreamWriter writer = new StreamWriter(filePath, true))
  152.                 writer.WriteLine("[" + DateTime.Now.Hour.ToString().PadLeft(2, '0') + ':' + DateTime.Now.Minute.ToString().PadLeft(2, '0') + '.' + DateTime.Now.Second.ToString().PadLeft(2, '0') + "] " + textBox1.Text);
  153.            
  154.             request.UploadFile(ftpFullPath, filePath);
  155.  
  156.             textBox1.Text = null;
  157.             Form1_Load(null, null);
  158.         }
  159.  
  160.         private void textBox1_TextChanged(object sender, EventArgs e)
  161.         {
  162.             if (textBox1.Text == "")
  163.                 button1.Enabled = false;
  164.             else
  165.                 button1.Enabled = true;
  166.         }
  167.  
  168.         private void richTextBox1_TextChanged(object sender, EventArgs e)
  169.         {
  170.             richTextBox1.SelectionStart = richTextBox1.Text.Length;
  171.             richTextBox1.ScrollToCaret();
  172.         }
  173.  
  174.         private void TestSize()
  175.         {
  176.             try
  177.             {
  178.                 requestDir = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpFullPath));
  179.                 requestDir.Credentials = new NetworkCredential(username, password);
  180.                 requestDir.Method = WebRequestMethods.Ftp.GetFileSize;
  181.                 response = (FtpWebResponse)requestDir.GetResponse();
  182.                 size = response.ContentLength;
  183.                 response.Close();
  184.             }
  185.             catch
  186.             {
  187.                 Form1_Load(null, null);
  188.             }
  189.         }
  190.  
  191.         private void timer1_Tick(object sender, EventArgs e)
  192.         {
  193.             TestSize();
  194.             if (size != sizeChange)
  195.             {
  196.                 Form1_Load(null, null);
  197.             }
  198.         }
  199.  
  200.         private void button2_Click(object sender, EventArgs e)
  201.         {
  202.             MessageBox.Show(size.ToString());
  203.             MessageBox.Show(sizeChange.ToString());
  204.         }
  205.     }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement