PDA

View Full Version : سوال: نمایش پیشرفت کپی فایلها با ProgressBar



pesare
پنج شنبه 05 تیر 1393, 14:35 عصر
سلام دوستان

نمایش پیشرفت کپی فایلها با ProgressBar به چه صورته؟

khokhan
پنج شنبه 05 تیر 1393, 14:53 عصر
سلام دوستان

نمایش پیشرفت کپی فایلها با ProgressBar به چه صورته:لبخند::لبخند::متفکر:؟

..................................................


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;

namespace WindowsApplication1 {
public partial class Form1 : Form {
// Class to report progress
private class UIProgress {
public UIProgress(string name_, long bytes_, long maxbytes_) {
name = name_; bytes = bytes_; maxbytes = maxbytes_;
}
public string name;
public long bytes;
public long maxbytes;
}
// Class to report exception {
private class UIError {
public UIError(Exception ex, string path_) {
msg = ex.Message; path = path_; result = DialogResult.Cancel;
}
public string msg;
public string path;
public DialogResult result;
}
private BackgroundWorker mCopier;
private delegate void ProgressChanged(UIProgress info);
private delegate void CopyError(UIError err);
private ProgressChanged OnChange;
private CopyError OnError;

public Form1() {
InitializeComponent();
mCopier = new BackgroundWorker();
mCopier.DoWork += Copier_DoWork;
mCopier.RunWorkerCompleted += Copier_RunWorkerCompleted;
mCopier.WorkerSupportsCancellation = true;
OnChange += Copier_ProgressChanged;
OnError += Copier_Error;
button1.Click += button1_Click;
ChangeUI(false);
}

private void Copier_DoWork(object sender, DoWorkEventArgs e) {
// Create list of files to copy
string[] theExtensions = { "*.jpg", "*.jpeg", "*.bmp", "*.png", "*.gif" };
List<FileInfo> files = new List<FileInfo>();
string path = Environment.GetFolderPath(Environment.SpecialFolde r.MyDocuments);
DirectoryInfo dir = new DirectoryInfo(path);
long maxbytes = 0;
foreach (string ext in theExtensions) {
FileInfo[] folder = dir.GetFiles(ext, SearchOption.AllDirectories);
foreach (FileInfo file in folder) {
if ((file.Attributes & FileAttributes.Directory) != 0) continue;
files.Add(file);
maxbytes += file.Length;
}
}
// Copy files
long bytes = 0;
foreach (FileInfo file in files) {
try {
this.BeginInvoke(OnChange, new object[] { new UIProgress(file.Name, bytes, maxbytes) });
File.Copy(file.FullName, @"c:\temp\" + file.Name, true);
}
catch (Exception ex) {
UIError err = new UIError(ex, file.FullName);
this.Invoke(OnError, new object[] { err });
if (err.result == DialogResult.Cancel) break;
}
bytes += file.Length;
}
}
private void Copier_ProgressChanged(UIProgress info) {
// Update progress
progressBar1.Value = (int)(100.0 * info.bytes / info.maxbytes);
label1.Text = "Copying " + info.name;
}
private void Copier_Error(UIError err) {
// Error handler
string msg = string.Format("Error copying file {0}\n{1}\nClick OK to continue copying files", err.path, err.msg);
err.result = MessageBox.Show(msg, "Copy error", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
}
private void Copier_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
// Operation completed, update UI
ChangeUI(false);
}
private void ChangeUI(bool docopy) {
label1.Visible = docopy;
progressBar1.Visible = docopy;
button1.Text = docopy ? "Cancel" : "Copy";
label1.Text = "Starting copy...";
progressBar1.Value = 0;
}
private void button1_Click(object sender, EventArgs e) {
bool docopy = button1.Text == "Copy";
ChangeUI(docopy);
if (docopy) mCopier.RunWorkerAsync();
else mCopier.CancelAsync();
}
}
}

pesare
پنج شنبه 05 تیر 1393, 15:01 عصر
نمونه پروژ هم دارید به من بدید؟
در حقیقت من دارم یه فایل رو Extract میکنم که میخوام اون رو با ProgressBar نمایش بدم

khokhan
پنج شنبه 05 تیر 1393, 15:11 عصر
نمونه پروژ هم دارید به من بدید؟
در حقیقت من دارم یه فایل رو Extract میکنم که میخوام اون رو با ProgressBar نمایش بدم

این لینک رو ببین http://www.codeproject.com/Articles/11850/Decompress-Zip-files-with-Windows-Shell-API-and-C

soleimani.em
پنج شنبه 05 تیر 1393, 15:19 عصر
سلام دوستان

نمایش پیشرفت کپی فایلها با ProgressBar به چه صورته؟



progressBar1.Maximum =100;
foreach (FileInfo tempfile in files)
{
progressBar1.PerformStep();
}

progressBar1.Value =0;

pesare
پنج شنبه 05 تیر 1393, 15:24 عصر
خطا داره

120512

pesare
پنج شنبه 05 تیر 1393, 15:32 عصر
files رو چطوری بایند کنم به فایل zip

pesare
پنج شنبه 05 تیر 1393, 15:34 عصر
progressBar1.Maximum =100;
foreach (FileInfo tempfile in files)
{
progressBar1.PerformStep();
}

progressBar1.Value =0;






files رو چطوری بایند کنم به فایل zip

pesare
پنج شنبه 05 تیر 1393, 15:58 عصر
با این کدها ProgressBar تا کمتر از نصف میاد


string folderPath = Application.StartupPath;
string zipFile = Application.StartupPath + @"\test.zip";


if (!File.Exists(zipFile))
throw new FileNotFoundException();


if (!Directory.Exists(folderPath))
Directory.CreateDirectory(folderPath);


Shell32.Shell objShell = new Shell32.Shell();
Shell32.Folder destinationFolder = objShell.NameSpace(folderPath);
Shell32.Folder sourceFile = objShell.NameSpace(zipFile);


foreach (var file in sourceFile.Items())
{


destinationFolder.CopyHere(file, 4 | 16);
progressBar1.PerformStep();
}


progressBar1.Value = 0;
}

shahryari
پنج شنبه 05 تیر 1393, 16:35 عصر
اینجا یه نمونه گذاشتم
http://www.w3-farsi.com/%DA%A9%D9%BE%DB%8C-%DA%A9%D8%B1%D8%AF%D9%86-%DB%8C%DA%A9-%D9%81%D8%A7%DB%8C%D9%84-%D8%AF%D8%B1-%D8%B3%DB%8C-%D8%B4%D8%A7%D8%B1%D9%BE-%D9%88-%D9%86%D9%85%D8%A7%DB%8C%D8%B4-%D9%88%D8%B6%D8%B9%DB%8C/