Transfer des fichier + l'utilisation des thread

Description

Ce code permet de transferer les fichier en local en convertissant les fichier en Bytes
avec un thread apart.....

Source / Exemple :


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Xml;
using System.Threading;

namespace BctDepository
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{		
		//private int currentOrthoDirName,currentPatientDirName,currentExamenDirName;		
		string pathFrom,pathTo;	
		private Thread thread = null;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Button browseSource;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Button browseDestination;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.OpenFileDialog openFile;
		private System.Windows.Forms.TextBox textSource;
		private System.Windows.Forms.TextBox txt;
		private System.Windows.Forms.TextBox textDestination;
		private System.Windows.Forms.Button exit;
		private System.Windows.Forms.SaveFileDialog saveFile;
		private System.Windows.Forms.Label fileSizeFromLabel;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.textSource = new System.Windows.Forms.TextBox();
			this.browseSource = new System.Windows.Forms.Button();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.textDestination = new System.Windows.Forms.TextBox();
			this.browseDestination = new System.Windows.Forms.Button();
			this.button1 = new System.Windows.Forms.Button();
			this.openFile = new System.Windows.Forms.OpenFileDialog();
			this.txt = new System.Windows.Forms.TextBox();
			this.exit = new System.Windows.Forms.Button();
			this.saveFile = new System.Windows.Forms.SaveFileDialog();
			this.fileSizeFromLabel = new System.Windows.Forms.Label();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.textSource);
			this.groupBox1.Controls.Add(this.browseSource);
			this.groupBox1.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(552, 48);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Source Path";
			// 
			// textSource
			// 
			this.textSource.Location = new System.Drawing.Point(8, 16);
			this.textSource.Name = "textSource";
			this.textSource.Size = new System.Drawing.Size(448, 20);
			this.textSource.TabIndex = 1;
			this.textSource.Text = "";
			// 
			// browseSource
			// 
			this.browseSource.Location = new System.Drawing.Point(464, 15);
			this.browseSource.Name = "browseSource";
			this.browseSource.TabIndex = 0;
			this.browseSource.Text = "Browse";
			this.browseSource.Click += new System.EventHandler(this.browseSource_Click);
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.textDestination);
			this.groupBox2.Controls.Add(this.browseDestination);
			this.groupBox2.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.groupBox2.Location = new System.Drawing.Point(8, 64);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(552, 48);
			this.groupBox2.TabIndex = 3;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Destination Path";
			// 
			// textDestination
			// 
			this.textDestination.Location = new System.Drawing.Point(8, 16);
			this.textDestination.Name = "textDestination";
			this.textDestination.Size = new System.Drawing.Size(448, 20);
			this.textDestination.TabIndex = 6;
			this.textDestination.Text = "";
			// 
			// browseDestination
			// 
			this.browseDestination.Location = new System.Drawing.Point(464, 15);
			this.browseDestination.Name = "browseDestination";
			this.browseDestination.TabIndex = 0;
			this.browseDestination.Text = "Browse";
			this.browseDestination.Click += new System.EventHandler(this.browseDestination_Click);
			// 
			// button1
			// 
			this.button1.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.button1.Location = new System.Drawing.Point(384, 120);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(96, 23);
			this.button1.TabIndex = 2;
			this.button1.Text = "Transfer";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// openFile
			// 
			this.openFile.Filter = "All Files (*.*) | *.*";
			// 
			// txt
			// 
			this.txt.Location = new System.Drawing.Point(0, 0);
			this.txt.Name = "txt";
			this.txt.TabIndex = 0;
			this.txt.Text = "";
			// 
			// exit
			// 
			this.exit.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.exit.Location = new System.Drawing.Point(484, 120);
			this.exit.Name = "exit";
			this.exit.TabIndex = 8;
			this.exit.Text = "Exit";
			this.exit.Click += new System.EventHandler(this.exit_Click);
			// 
			// saveFile
			// 
			this.saveFile.Filter = "All Files (*.*) | *.*";
			// 
			// fileSizeFromLabel
			// 
			this.fileSizeFromLabel.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.fileSizeFromLabel.Location = new System.Drawing.Point(8, 120);
			this.fileSizeFromLabel.Name = "fileSizeFromLabel";
			this.fileSizeFromLabel.Size = new System.Drawing.Size(360, 16);
			this.fileSizeFromLabel.TabIndex = 9;
			this.fileSizeFromLabel.Text = "File Size ( 0.0 Ko)";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(570, 152);
			this.Controls.Add(this.fileSizeFromLabel);
			this.Controls.Add(this.exit);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.button1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
			this.Name = "Form1";
			this.Text = "Transfer Files By Converting It\'s to Bytes";
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void browseSource_Click(object sender, System.EventArgs e) {
			if (openFile.ShowDialog()==DialogResult.OK){
				textSource.Text=openFile.FileName;
				}				
				FileStream sizeFile = new FileStream(textSource.Text,FileMode.Open,FileAccess.Read);
				fileSizeFromLabel.Text="File Size ( "+(sizeFile.Length/1024) +" Ko)";
				sizeFile.Close();
				
				
		}

		private void button1_Click(object sender, System.EventArgs e) {				
			
			try{
			pathFrom = textSource.Text ;
			pathTo = textDestination.Text;			
				ThreadStart threadStart = new ThreadStart(lunchThread);
				thread = new Thread(threadStart);		
				thread.Start();			
			}catch(Exception ex)
			{
			MessageBox.Show(ex.ToString());
			}
		}
		private void lunchThread()
		{
			transferFile(pathFrom,pathTo);
		}
		
		private void transferFile(string pathFrom,string pathTo)
		{								
			try{
			FileStream streamRead = new FileStream(pathFrom,FileMode.Open,FileAccess.Read);
			FileStream streamWriter = new FileStream(pathTo,FileMode.CreateNew,FileAccess.ReadWrite);
			int len = (int)streamRead.Length;				
			Byte[] byteFile = new Byte[len];
			streamRead.Read(byteFile,0,len);			
			streamWriter.Write(byteFile,0,byteFile.Length);			
			
			streamRead.Close();	
			streamWriter.Close();		
			MessageBox.Show("Transfered success","Crack_xp Demo",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);	
			}catch (Exception ex){
				MessageBox.Show(ex.ToString(),"Exception in Transfer File",MessageBoxButtons.OK,MessageBoxIcon.Warning);
				MessageBox.Show("Transfered Filed!!","Crack_xp Demo",MessageBoxButtons.OK,MessageBoxIcon.Error);	
			}
					
		}	
		
		private void exit_Click(object sender, System.EventArgs e) {
			Application.Exit();
		}

		private void browseDestination_Click(object sender, System.EventArgs e) {
			try{
			saveFile.FileName=textSource.Text.Remove(0,textSource.Text.LastIndexOf("\\"));
			if (saveFile.ShowDialog()==DialogResult.OK){
				textDestination.Text=saveFile.FileName;
				}
			}catch(Exception ex)	
			{
				MessageBox.Show(ex.ToString(),"Exception in Transfer File",MessageBoxButtons.OK,MessageBoxIcon.Warning);
			}
		}
		
	}
}

Conclusion :


t'es content mainteant SimonKari ??!!!!!

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.