Advertisement
Guest User

FTP Tchat by SpyLX

a guest
Jan 19th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.44 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.             // Envoi du nouveau message écrit sur le textBox1
  152.             using (StreamWriter writer = new StreamWriter(filePath, true))
  153.                 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);
  154.            
  155.             request.UploadFile(ftpFullPath, filePath);
  156.  
  157.             textBox1.Text = null;
  158.             Form1_Load(null, null);
  159.         }
  160.  
  161.         private void textBox1_TextChanged(object sender, EventArgs e)
  162.         {
  163.             // Griser le button1 si le textBox1 est vide
  164.             if (textBox1.Text == "")
  165.                 button1.Enabled = false;
  166.             else
  167.                 button1.Enabled = true;
  168.         }
  169.  
  170.         private void richTextBox1_TextChanged(object sender, EventArgs e)
  171.         {
  172.             // Aller en bas du richTextBox1 quand il y a un nouveau message
  173.             richTextBox1.SelectionStart = richTextBox1.Text.Length;
  174.             richTextBox1.ScrollToCaret();
  175.         }
  176.  
  177.         private void TestSize()
  178.         {
  179.             // Obtenir la taille du fichier d'aujourd'hui sur le serveur FTP
  180.             try
  181.             {
  182.                 requestDir = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpFullPath));
  183.                 requestDir.Credentials = new NetworkCredential(username, password);
  184.                 requestDir.Method = WebRequestMethods.Ftp.GetFileSize;
  185.                 response = (FtpWebResponse)requestDir.GetResponse();
  186.                 size = response.ContentLength;
  187.                 response.Close();
  188.             }
  189.             catch
  190.             {
  191.                 Form1_Load(null, null);
  192.             }
  193.         }
  194.  
  195.         private void timer1_Tick(object sender, EventArgs e)
  196.         {
  197.             // Tester toutes les 1000 ticks si le fichier d'aujourd'hui du serveur FTP a été modifié, si oui, actualiser
  198.             TestSize();
  199.             if (size != sizeChange)
  200.             {
  201.                 Form1_Load(null, null);
  202.             }
  203.         }
  204.  
  205.         private void button2_Click(object sender, EventArgs e)
  206.         {
  207.             MessageBox.Show(size.ToString());
  208.             MessageBox.Show(sizeChange.ToString());
  209.         }
  210.     }//ce soir : Mettre les nouveaux commentaires, et voir pour le bug du serv introuvable, + faire les trucs sur keep
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement