Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO;
- using System.Net;
- namespace FTP_Tchat_by_SpyLX
- {
- public partial class Form1 : Form
- {
- private string localDirectory;
- private string filePath;
- private string fileNameToday;
- private string ftpFullPath;
- private string username;
- private string password;
- private string[] messages;
- private short tryAgainOrError;
- private long size;
- private long sizeChange;
- private WebClient request;
- private FtpWebRequest requestDir;
- private FtpWebResponse response;
- public Form1()
- {
- InitializeComponent();
- localDirectory = @"C:\ProgramData\cmd_tchat\";
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- up:
- string date = (DateTime.Now.Day + "." + DateTime.Now.Month + "." + DateTime.Now.Year);
- fileNameToday = localDirectory + date + ".txt";
- // Vérification si le fichier d'aujourd'hui existe, sinon le créer
- try
- {
- if (!File.Exists(fileNameToday))
- {
- File.Create(fileNameToday).Close();
- goto up;
- }
- else
- {
- filePath = (fileNameToday);
- }
- }
- catch (DirectoryNotFoundException)
- {
- Directory.CreateDirectory(localDirectory.Remove(localDirectory.Length - 1));
- goto up;
- }
- string ftpHost;
- // Récupération des valeurs de ftp_settings
- try
- {
- ftpHost = File.ReadAllLines(localDirectory + "ftp_settings")[2];
- ftpFullPath = "ftp://" + ftpHost + "/" + date + ".txt";
- username = File.ReadAllLines(localDirectory + "ftp_settings")[3];
- password = File.ReadAllLines(localDirectory + "ftp_settings")[4];
- if (username == "-")
- username = "";
- else if (password == "-")
- password = "";
- }
- catch (FileNotFoundException)
- {
- File.Create(localDirectory + "ftp_settings").Close();
- using (StreamWriter writer = new StreamWriter(localDirectory + "ftp_settings", true))
- {
- writer.WriteLine("# Line 3: IP Adress ; Line 4: FTP Username ; Line 5: FTP Password");
- writer.WriteLine("# Put a '-' if the value is null");
- writer.WriteLine("127.0.0.1");
- writer.WriteLine("spylx");
- writer.WriteLine("-");
- writer.Close();
- }
- goto up;
- }
- catch (IndexOutOfRangeException)
- {
- MessageBox.Show("Une valeur est vide dans " + localDirectory + "ftp_settings");
- Application.Exit();
- goto up;
- }
- // Téléchargement du fichier d'aujourd'hui
- using (request = new WebClient())
- {
- request.Credentials = new NetworkCredential(username, password);
- TestSize();
- sizeChange = size;
- byte[] fileData;
- // Prendre le fichier d'aujourd'hui, si il n'existe pas, le créer sur le serveur FTP
- try
- {
- fileData = request.DownloadData(ftpFullPath);
- }
- catch (WebException)
- {
- try
- {
- request.UploadFile(ftpFullPath, filePath);
- goto up;
- }
- catch (WebException)
- {
- tryAgainOrError++;
- if (tryAgainOrError <= 100)
- {
- goto up;
- } else
- {
- MessageBox.Show("Impossible d'accéder au serveur.");
- Application.Exit();
- }
- }
- return;
- }
- // Écriture du fichier téléchargé ci-dessus
- using (FileStream file = File.Create(filePath))
- {
- file.Write(fileData, 0, fileData.Length);
- file.Close();
- }
- }
- // Ajout du texte du fichier d'aujourd'hui dans RichTextBox1
- messages = File.ReadAllLines(filePath, Encoding.UTF8);
- richTextBox1.Lines = messages;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- // Envoi du nouveau message écrit sur le textBox1
- using (StreamWriter writer = new StreamWriter(filePath, true))
- 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);
- request.UploadFile(ftpFullPath, filePath);
- textBox1.Text = null;
- Form1_Load(null, null);
- }
- private void textBox1_TextChanged(object sender, EventArgs e)
- {
- // Griser le button1 si le textBox1 est vide
- if (textBox1.Text == "")
- button1.Enabled = false;
- else
- button1.Enabled = true;
- }
- private void richTextBox1_TextChanged(object sender, EventArgs e)
- {
- // Aller en bas du richTextBox1 quand il y a un nouveau message
- richTextBox1.SelectionStart = richTextBox1.Text.Length;
- richTextBox1.ScrollToCaret();
- }
- private void TestSize()
- {
- // Obtenir la taille du fichier d'aujourd'hui sur le serveur FTP
- try
- {
- requestDir = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpFullPath));
- requestDir.Credentials = new NetworkCredential(username, password);
- requestDir.Method = WebRequestMethods.Ftp.GetFileSize;
- response = (FtpWebResponse)requestDir.GetResponse();
- size = response.ContentLength;
- response.Close();
- }
- catch
- {
- Form1_Load(null, null);
- }
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- // Tester toutes les 1000 ticks si le fichier d'aujourd'hui du serveur FTP a été modifié, si oui, actualiser
- TestSize();
- if (size != sizeChange)
- {
- Form1_Load(null, null);
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- MessageBox.Show(size.ToString());
- MessageBox.Show(sizeChange.ToString());
- }
- }//ce soir : Mettre les nouveaux commentaires, et voir pour le bug du serv introuvable, + faire les trucs sur keep
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement