1398-05-14 01:44 عمل کپی فایل با Progress bar
zahmah

 
سلام
تابع زیر عمل کپی رو به همراه نمایش Progress bar استفاده میکنم
ولی هنگامی که پوشه رو کپی میکنم یا تعداد زیادی فایل؛ این ارور رو میده:
"Value of '2' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.\r\nParameter name: Value"

اصل تابع:
public static void CopyFile(string FileSource, string FileDestination, ProgressBar progressBar1)
{
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) ;
FileInfo aaaa = new FileInfo(FileSource) ;
Int64 a = aaaa.Length ;
Int64 a1 = a /= 1000 ;
//MessageBox.Show(a1.ToString()) ;

progressBar1.Maximum = int.Parse(a1.ToString()) ;
progressBar1.Value = progressBar1.Value + (NumRead /= 1000) ;

Writer.Flush() ;
}

From.Close() ;
To.Close() ;
if (progressBar1.Value > 99)
{
progressBar1.Value = 0 ;
//MessageBox.Show( "Copy Finished successfuly" ) ;
}
}


مشکل از کجاست؟
ممنون میشم راهنمایی بفرمائین.
1398-05-15 00:08 کد سی شارپ برای کپی یک فایل با نمایش نوار پیشرفت/ProgressBar
حاجی شریفی
مؤسس سایت
 
سلام
این کد را امتحان کنید:
public static void  CopyFile(string  FileSource, string  FileDestination, ProgressBar progressBar1)
{
const int BUFFER_SIZE = 64 * 1024 ;

progressBar1.Minimum = 0 ;
progressBar1.Maximum = 100 ;
progressBar1.Value = 0 ;

using (var From = new System.IO.FileStream(FileSource, System.IO.FileMode.Open, System.IO.FileAccess.Read))
using (var To = new System.IO.FileStream(FileDestination, System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
long SumCopy = 0 ;
long FileLength = From.Length ;
if (FileLength <= 0) return ;

byte [] buffer = new byte [BUFFER_SIZE] ;
while (true )
{
int NumRead = From.Read(buffer, 0, BUFFER_SIZE) ;
if (NumRead <= 0) break ; //END

To.Write(buffer, 0, NumRead) ;
SumCopy += NumRead ;

progressBar1.Value = (int )(100 * SumCopy / FileLength) ;
}
}

//MessageBox.Show( "Copy Finished successfuly" ) ;
}