Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Kursovy_proekt_Sribny_V_A_1234 (Автосохраненный...docx
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
341.65 Кб
Скачать

Додаток б

Лістинг основного модуля програми

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Threading;

using System.IO;

using System.Windows.Forms;

namespace SpeedSearch

{

public partial class CopyForm : Form

{

FolderBrowserDialog fbd = new FolderBrowserDialog();

List<string> fromPath = new List<string>();

List<string> toPath = new List<string>();

long dataLen = 0;

long progress = 0;

long lastPos = 0;

int substringIndex = 0;

int end = -1;

System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();

Thread copyThread = null;

void select(FileInformation fi)

{

fromPath.Add(fi.FullPath);

toPath.Add(fi.FullPath.Substring(substringIndex));

dataLen += fi.Size;

}

protected override void OnLoad(EventArgs e)

{

base.OnLoad(e);

Program.LoadFormState(this);

try

{

comboBox_path.Items.AddRange(File.ReadAllLines(Program.ConfigPath + "CopyHistory.txt"));

}

catch

{

}

if (comboBox_path.Items.Count > 0)

{

comboBox_path.SelectedIndex = 0;

}

}

protected override void OnClosed(EventArgs e)

{

base.OnClosed(e);

Program.SaveFormState(this);

try

{

StreamWriter sw = new StreamWriter(Program.ConfigPath + "CopyHistory.txt");

try

{

foreach (string s in comboBox_path.Items)

{

sw.WriteLine(s);

}

}

finally

{

if (sw != null) sw.Close();

}

}

catch

{

}

try

{

copyThread.Abort();

}

catch

{

}

}

public CopyForm(string[] items)

{

InitializeComponent();

for (int n = 0; n < items.Length; n++)

{

if (File.Exists(items[n]))

{

fromPath.Add(items[n]);

toPath.Add(Path.GetFileName(items[n]));

try

{

dataLen += new FileInfo(items[n]).Length;

}

catch

{

}

}

if (Directory.Exists(items[n]))

{

substringIndex = items[n].Length;

FileSystemServices.Select(items[n], 7777, false, true, select);

}

}

label_file.Text = "Всего: " + fromPath.Count;

}

long copyFile(string from, string to)

{

long r = 0;

long cl = 0;

try

{

if (!Directory.Exists(Path.GetDirectoryName(to)))

{

Directory.CreateDirectory(Path.GetDirectoryName(to));

}

FileStream a = new FileStream(from, FileMode.Open);

FileStream b = new FileStream(to, FileMode.Create);

r = a.Length;

byte[] buf = new byte[1024 * 8];

try

while (a.Position < a.Length)

{

int rl = a.Read(buf, 0, buf.Length);

b.Write(buf, 0, rl);

cl += rl;

progress += rl;

progressBar1.Value = (int)(progress / 10000);

}

}

catch

{

progress -= cl;

r = -r;

}

finally

{

a.Close();

b.Close();

}

}

catch

{

r = -r;

}

return r;

}

void copy()

{

for (int n = 0; n < fromPath.Count; n++)

{

label_file.Text = Path.GetFileName(fromPath[n]);

long sr = copyFile(fromPath[n], comboBox_path.Text + "\\" + toPath[n]);

if (sr < 0)

{

if (File.Exists(fromPath[n]))

{

switch (MessageBox.Show("Неудалось копировать: " + Path.GetFileName(fromPath[n])

+ "\nВ папку: " + Path.GetDirectoryName(comboBox_path.Text + "\\" +

+ "\n\nПовторить?"

, "Ошибка копирования",

MessageBoxButtons.YesNo, MessageBoxIcon.Question))

{

case DialogResult.Yes:

n--;

break;

default:

progress += -sr;

break;

}

}

}

}

label_file.Text = "Готово!";

label_time.Text = "";

label_speed.Text = "";

end = 0;

button_cancel.Enabled = false;

}

private void button_copy_Click(object sender, EventArgs e)

{

button_copy.Enabled= button_selectPath.Enabled = comboBox_path.Enabled = false;

progressBar1.Minimum= 0;

progressBar1.Value = 0;

progressBar1.Maximum = (int)(dataLen / 10000);

try

{

sw.Start();

copyThread = new Thread(copy);

copyThread.Start();

int ttt = comboBox_path.Items.IndexOf(comboBox_path.Text);

if (ttt < 0)

{

comboBox_path.Items.Insert(0, comboBox_path.Text);

if (comboBox_path.Items.Count > 33)

{

comboBox_path.Items.RemoveAt(comboBox_path.Items.Count - 1);

}

}

timer1000.Enabled = true;

button_copy.Visible = false;

}

catch

{

MessageBox.Show("Ошибка, повторите...");

button_copy.Enabled = button_selectPath.Enabled = comboBox_path.Enabled = true;

}

}

private void button_cancel_Click(object sender, EventArgs e)

{

Close();

}

private void timer1000_Tick(object sender, EventArgs e)

{

if (end < 0)

{

sw.Stop();

long dp = progress - lastPos;

long t = dp != 0 ? (long)((dataLen - progress) * (sw.ElapsedMilliseconds / 1000F)) / dp : 0;

label_time.Text = new TimeSpan(0, 0, (int)t + 1).ToString();

float tdp = tdp = (dp / 1024 / 1024) / (sw.ElapsedMilliseconds / 1000F);

if (tdp != float.NaN)

{

label_speed.Text = tdp.ToString("00.00") + " MB/S";

}

else

{

label_speed.Text = "Расчет...";

}

lastPos = progress;

sw.Reset();

sw.Start();

float per = (float)progress / (float)dataLen * 100;

this.Text = "Копирование: " + per.ToString("00.00") + "%";

}

else

{

this.Text = "Копирование завершено";

end++;

button_cancel.Text = "Выход (" + (3 - end - 1) + ")";

if (end == 3)

{

Thread.Sleep(111);

Close();

}

}

}

private void comboBox1_TextChanged(object sender, EventArgs e)

{

button_copy.Enabled = Directory.Exists(comboBox_path.Text);

}

private void button_selectPath_Click(object sender, EventArgs e)

{

if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)

{

comboBox_path.Text = fbd.SelectedPath;

}

}

}

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]