PDA

View Full Version : سوال: نمایش progress bar برای کپی فایل



mtsoft
شنبه 14 اردیبهشت 1392, 06:37 صبح
سلام خدمت همه اساتید محترم

میشه من رو راهنمایی بفرمایید که چه جوری میتونم پروسه کپی فایل رو با یک progressbar نمایش بدم مثل پنجره کپی ویندوز .

mazaher5723
شنبه 14 اردیبهشت 1392, 07:55 صبح
با استفاده از Background Worker انجامش بده.

returnx
شنبه 14 اردیبهشت 1392, 09:32 صبح
فایل را به صورت بایت به بایت(یا آرایه ای از بایت ها) انتقال بدید و از فرمول زیر برای محاسبه Value برای Progress Bar استفاده کنید :
Value=(Byte Transfer * 100) / File Length

کامبیز اسدزاده
شنبه 14 اردیبهشت 1392, 09:50 صبح
جستجو کنید...


public partial class Form1 : Form { public Form1() { InitializeComponent(); Shown += new EventHandler(Form1_Shown); // To report progress from the background worker we need to set this property backgroundWorker1.WorkerReportsProgress = true; // This event will be raised on the worker thread when the worker starts backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); // This event will be raised when we call ReportProgress backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_Prog ressChanged); } void Form1_Shown(object sender, EventArgs e) { // Start the background worker backgroundWorker1.RunWorkerAsync(); } // On worker thread so do our thing! void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { // Your background task goes here for (int i = 0; i <= 100; i++) { // Report progress to 'UI' thread backgroundWorker1.ReportProgress(i); // Simulate long task System.Threading.Thread.Sleep(100); } } // Back on the 'UI' thread so we can update the progress bar void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { // The progress percentage is a property of e progressBar1.Value = e.ProgressPercentage; } }

FastCode
شنبه 14 اردیبهشت 1392, 10:18 صبح
Max = File Size / System Page Size
Value = Copied / System Page Size
معمولا اینطوری انجام میشه.

http://pinvoke.net/default.aspx/kernel32/GetSystemInfo.html

keyvan_s89
شنبه 14 اردیبهشت 1392, 11:16 صبح
با سلام

یک دکمه بزارید و اینو توش بنویسید.

openFileDialog1.ShowDialog();
folderBrowserDialog1.ShowDialog();
System.IO.FileInfo d=new System.IO.FileInfo(openFileDialog1.FileName.ToStri ng());
CopyFile(openFileDialog1.FileName.ToString(), folderBrowserDialog1.SelectedPath.ToString()+"\\"+ d.Name.ToString());

بعد یه تابع تعریف میکنید

public void CopyFile(string FileSource, string FileDestination)
{
int NumRead;
long FileLength;
System.IO.FileStream From = new System.IO.FileStream(FileSource, System.IO.FileMode.Open);
System.IO.FileStream To = new System.IO.FileStream(FileDestination, System.IO.FileMode.CreateNew);
byte[] buffer = new byte[1024];
FileLength = From.Length;
progressBar1.Minimum = 0;
progressBar1.Maximum = (int)FileLength;
while (FileLength > 0)
{
System.IO.BinaryReader Reader = new System.IO.BinaryReader(From);
NumRead = Reader.Read(buffer, 0, 1024);
FileLength = FileLength - NumRead;
System.IO.BinaryWriter Writer = new System.IO.BinaryWriter(To);
Writer.Write(buffer, 0, NumRead);
progressBar1.Value = progressBar1.Value + NumRead;
Writer.Flush();
}
From.Close();
To.Close();
if (progressBar1.Value > 99)
{
progressBar1.Value = 0;
MessageBox.Show("Copy Finished successfuly");
}
همین.
موفق باشید