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.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Threading;
- namespace CopyFilesDialog
- {
- public partial class CopyDialog : Form
- {
- public CopyDialog()
- {
- InitializeComponent();
- }
- private delegate void InCp(string fileName);
- private Thread newTh;
- private void btnCopy_Click(object sender, EventArgs e)
- {
- OpenFileDialog Fd = new OpenFileDialog();
- Fd.Title = "Выберите файл";
- Fd.InitialDirectory = @"C:\";
- Fd.Multiselect = true;
- string[] files;
- if (Fd.ShowDialog() == DialogResult.OK)
- {
- files = Fd.FileNames;
- progresPart.Maximum = files.Length;
- progresPart.Value = 0;
- newTh = new Thread(new ParameterizedThreadStart(CopyFiles));
- newTh.Start(files);
- }
- }
- private void CopyFiles(object _files)
- {
- string[] files = (string[])_files;
- foreach (string fi in files)
- {
- cp(fi);
- }
- }
- private void cp(string path)
- {
- if (progresPart.InvokeRequired)
- {
- InCp add = new InCp(cp);
- this.Invoke(add, new object[] { path });
- }
- else
- {
- string dest = @"C:\Temp";
- try
- {
- if (Directory.Exists(path) == false)
- {
- if (File.Exists(path) == true)
- {
- string[] pathSplit = path.Split(new Char[] { '\\' });
- string lastFile = pathSplit[pathSplit.Length - 1];
- File.Copy(path, dest + '\\' + lastFile);
- }
- }
- else
- {
- //рекурсивное копирование папки
- string[] pathSplit = path.Split(new Char[] { '\\' });
- string lastDir = pathSplit[pathSplit.Length - 1];
- string ToDir = dest + '\\' + lastDir;
- CopyDir(path, ToDir);
- }
- progresPart.Value++;
- progresPart.Update();
- }
- catch (Exception ex)
- {
- MessageBox.Show("Ошибка: " + ex.Message);
- }
- progresPart.Update();
- }
- }
- //рекурсивное копирование папки
- void CopyDir(string FromDir, string ToDir)
- {
- Directory.CreateDirectory(ToDir);
- foreach (string s1 in Directory.GetFiles(FromDir))
- {
- string s2 = ToDir + "\\" + Path.GetFileName(s1);
- File.Copy(s1, s2);
- }
- foreach (string s in Directory.GetDirectories(FromDir))
- {
- CopyDir(s, ToDir + "\\" + Path.GetFileName(s));
- }
- }
- private void btnCancel_Click(object sender, EventArgs e)
- {
- if (newTh.IsAlive)
- newTh.Abort();
- btnCancel.Visible = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement