Advertisement
Guest User

Untitled

a guest
Oct 27th, 2012
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.52 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.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9. using System.Threading;
  10.  
  11. namespace CopyFilesDialog
  12. {
  13.     public partial class CopyDialog : Form
  14.     {
  15.         public CopyDialog()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         private delegate void InCp(string fileName);
  21.         private Thread newTh;
  22.  
  23.         private void btnCopy_Click(object sender, EventArgs e)
  24.         {
  25.             OpenFileDialog Fd = new OpenFileDialog();
  26.             Fd.Title = "Выберите файл";
  27.             Fd.InitialDirectory = @"C:\";
  28.             Fd.Multiselect = true;
  29.  
  30.             string[] files;
  31.             if (Fd.ShowDialog() == DialogResult.OK)
  32.             {
  33.                 files = Fd.FileNames;
  34.                 progresPart.Maximum = files.Length;
  35.  
  36.                 progresPart.Value = 0;
  37.                 newTh = new Thread(new ParameterizedThreadStart(CopyFiles));
  38.                 newTh.Start(files);
  39.             }
  40.         }
  41.  
  42.         private void CopyFiles(object _files)
  43.         {
  44.             string[] files = (string[])_files;
  45.             foreach (string fi in files)
  46.             {
  47.                 cp(fi);
  48.             }
  49.         }
  50.  
  51.         private void cp(string path)
  52.         {
  53.             if (progresPart.InvokeRequired)
  54.             {
  55.                 InCp add = new InCp(cp);
  56.                 this.Invoke(add, new object[] { path });
  57.             }
  58.             else
  59.             {
  60.                 string dest = @"C:\Temp";
  61.                 try
  62.                 {
  63.                     if (Directory.Exists(path) == false)
  64.                     {
  65.                         if (File.Exists(path) == true)
  66.                         {
  67.                             string[] pathSplit = path.Split(new Char[] { '\\' });
  68.                             string lastFile = pathSplit[pathSplit.Length - 1];
  69.                             File.Copy(path, dest + '\\' + lastFile);
  70.                         }
  71.                     }
  72.                     else
  73.                     {
  74.                         //рекурсивное копирование папки
  75.                         string[] pathSplit = path.Split(new Char[] { '\\' });
  76.                         string lastDir = pathSplit[pathSplit.Length - 1];
  77.                         string ToDir = dest + '\\' + lastDir;
  78.                         CopyDir(path, ToDir);
  79.                     }
  80.                     progresPart.Value++;
  81.                     progresPart.Update();
  82.                 }
  83.                 catch (Exception ex)
  84.                 {
  85.                     MessageBox.Show("Ошибка: " + ex.Message);
  86.                 }
  87.  
  88.                 progresPart.Update();
  89.             }
  90.         }
  91.  
  92.  
  93.         //рекурсивное копирование папки
  94.         void CopyDir(string FromDir, string ToDir)
  95.         {
  96.             Directory.CreateDirectory(ToDir);
  97.             foreach (string s1 in Directory.GetFiles(FromDir))
  98.             {
  99.                 string s2 = ToDir + "\\" + Path.GetFileName(s1);
  100.                 File.Copy(s1, s2);
  101.             }
  102.             foreach (string s in Directory.GetDirectories(FromDir))
  103.             {
  104.                 CopyDir(s, ToDir + "\\" + Path.GetFileName(s));
  105.             }
  106.         }
  107.  
  108.         private void btnCancel_Click(object sender, EventArgs e)
  109.         {
  110.             if (newTh.IsAlive)
  111.                 newTh.Abort();
  112.             btnCancel.Visible = false;
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement