PDA

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



Ship Storm
جمعه 17 آذر 1391, 08:59 صبح
سلام دوستان و اساتید
من یک سوال دارم ممنون میشم راهنمایی بفرمائید
یک دکمه گذاشتم تو فرم خودم برای کپی کردن فایل ها
کدی هم که توش نوشتم اینه :





If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
IO.File.Copy(OpenFileDialog1.FileName, "C:\project copy\" & IO.Path.GetFileName(OpenFileDialog1.FileName), True)
End If

حالا میخوام یک ProgressBar هم به فرمم اضافه کنم تا میزان پیشرفت رو در زمان کپی شدن فایل هنگامی که روی دکمه Copy کلیک میکنم ببینم

Ship Storm
جمعه 17 آذر 1391, 12:07 عصر
یک سوال دیگه هم دارم اینکه
چطور میتونم حجم فایلی که از این طریق انتخاب شده رو بدست بیارم و تو یک لیبل نمایش بدم ؟
یک کدنوشتم ولی مسیر رو فقط دستی بهش میدی که حجم رو برمیگردونه :



Private Function GetFileSize(ByVal MyFilePath As String) As
Long
Dim MyFile As New FileInfo(MyFilePath)
Dim FileSize As Long =
MyFile.Length
Return FileSize




Private Sub Button2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button2.Click
Dim temp As String
temp =
GetFileSize("C:\Users\HAMED\Videos\1.mp4") / 1000
MsgBox(temp + "KB")
End
Sub

Ship Storm
جمعه 17 آذر 1391, 12:23 عصر
مشکل حجم فایل برطرف شد با این دستور :




Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog =
Windows.Forms.DialogResult.OK Then
IO.File.Copy(OpenFileDialog1.FileName,
"C:\project copy\" & IO.Path.GetFileName(OpenFileDialog1.FileName),
True)
If OpenFileDialog1.Multiselect Then
Dim total As Long = 0
For
Each s As String In OpenFileDialog1.FileNames
total += New
FileInfo(s).Length
Next
Label2.Text =
(total.ToString())
Else
Label2.Text = (New
FileInfo(OpenFileDialog1.FileName).Length.ToString ())
End If
End If
End
Sub

alimanam
جمعه 17 آذر 1391, 14:59 عصر
با سلام

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


Imports System.IO

Public Class Form1

Private Sub btnSelectFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectFile.Click
Dim ofd As New OpenFileDialog With {.Filter = "All Files|*.*"}
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
txtFileDes.Text = ofd.FileName
txtFileSaveWhere.Text = ""
lblFileSize.Text = Math.Round(CInt(New FileInfo(ofd.FileName).Length.ToString) / 1024, 3) & " کیلو بایت "
End If
End Sub

Private Sub btnWhereToSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWhereToSave.Click
Dim sfd As New FolderBrowserDialog
If txtFileDes.Text.Length > 0 AndAlso sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
txtFileSaveWhere.Text = sfd.SelectedPath & "\" & New FileInfo(txtFileDes.Text).Name
End If
End Sub

Private Sub SaveBinaryFile(ByVal strFilename As String, ByVal bytesToWrite() As Byte)
Dim position As Integer = 0
Dim BufferSize As Integer = 4096

ProgressBar1.Value = 0
Using fsNew As FileStream = New FileStream(strFilename, FileMode.Create, FileAccess.Write)
Do
Dim intToCopy As Integer = Math.Min(BufferSize, bytesToWrite.Length - position)
Dim buffer(intToCopy - 1) As Byte
Array.Copy(bytesToWrite, position, buffer, 0, intToCopy)
fsNew.Write(buffer, 0, buffer.Length)
ProgressBar1.Value = ((position / bytesToWrite.Length) * 100)
lblAction.Text = "در حال کپی کردن فایل"
ProgressBar1.Refresh()
Application.DoEvents()
position += intToCopy
Loop While position < bytesToWrite.Length
ProgressBar1.Value = 0
lblAction.Text = ""
End Using
End Sub

Private Function ReadBinaryFile(ByVal strFilename As String) As Byte()
Dim position As Integer = 0
Dim bufferSize As Integer = 4096
Dim bytes() As Byte

ProgressBar1.Value = 0
Using fsOpen As FileStream = New FileStream(strFilename, FileMode.Open)
ReDim bytes((fsOpen.Length) - 1)
Do
If (position + bufferSize) > fsOpen.Length Then
fsOpen.Read(bytes, position, fsOpen.Length - position)
Exit Do
Else
fsOpen.Read(bytes, position, bufferSize)
End If
ProgressBar1.Value = ((position / fsOpen.Length) * 100)
lblAction.Text = "درحال خواندن فایل ..."
ProgressBar1.Refresh()
Application.DoEvents()
position += bufferSize
Loop
ProgressBar1.Value = 0
lblAction.Text = "آماده کپی کردن فایل"
End Using
Return bytes
End Function

Private Sub btnCopyFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopyFile.Click
If txtFileDes.Text.Length > 0 AndAlso txtFileSaveWhere.Text.Length > 0 Then
SaveBinaryFile(txtFileSaveWhere.Text, ReadBinaryFile(txtFileDes.Text))
End If
End Sub

End Class



موفق باشید./

Ship Storm
جمعه 17 آذر 1391, 18:49 عصر
سلام مجدد
ممنونم واقعا جای تشکر و قدردانی داره درست شد و بخوبی کار میکنه
فقط یک سوال دیگه هم دارم
آیا میشه دکمه Pause و Resume به این برنامه اضافه کرد که وقتی داره کپی میکنه Pause رو بزنی کپی متوقف بشه و وقتی Resume رو میزنی کپی مجددا ادامه پیدا کنه /؟

Ship Storm
شنبه 18 آذر 1391, 20:32 عصر
سلام مجدد بر اساتید گرامی
من دوتا سوال دارم در مورد این برنامه
اول اینکه چطور میشه read و write رو همزمان کنم ؟
دوم اینکه اگر بخوام از thread یا backgroundworker هم استفاده کنم تا مثلا الان فرم قفل نکنه چه تغییراتی باید بدم ؟
ممنون و با تشکر

shahryari
شنبه 18 آذر 1391, 22:54 عصر
با سلام
يك تابع ساده هم من نوشتم كه اگه 3 تا پارامتر زير رو بهش بدي مثل ساعت !! كار ميكنه
1- فايلي كه قراره كپي بشه : مثلا TextBox1.text يا "C:\temp\test.mp3"
2- مسير نام كه قراره با اون نام و در آن مسير كپي بشه : مثلا TextBox2.Text يا "D:\temp\test.mp3"
3- يك كنترل پروگرس بار: مثلا ProgressBar1
-------------------------------------------
اين هم تابع:

Public Sub Copyfile(ByVal sourceFileName As String, ByVal targetFileName As String, ByVal progress As ProgressBar)
Dim S_file As New IO.FileStream(sourceFileName, IO.FileMode.Open)
Dim D_file As New IO.FileStream(targetFileName, IO.FileMode.Create)
Dim len As Long = S_file.Length - 1
For i As Long = 0 To len
D_file.WriteByte(S_file.ReadByte)
If i Mod 1000 = 0 Then
progress.Value = i * 100 / len
Application.DoEvents()
End If
Next
S_file.Close()
D_file.Close()
progress.Value = 0
End Sub
---------------------
روش استفاده از تابع با يك باتن

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If TextBox1.Text <> Nothing Or TextBox2.Text <> Nothing Then
Copyfile(TextBox1.Text, TextBox2.Text, ProgressBar1)
End If
End Sub
موفق باشيد

Hossis
شنبه 18 آذر 1391, 23:06 عصر
سلام مجدد
ممنونم واقعا جای تشکر و قدردانی داره درست شد و بخوبی کار میکنه
فقط یک سوال دیگه هم دارم
آیا میشه دکمه Pause و Resume به این برنامه اضافه کرد که وقتی داره کپی میکنه Pause رو بزنی کپی متوقف بشه و وقتی Resume رو میزنی کپی مجددا ادامه پیدا کنه /؟
باید برای این کار, از حلقه For I=0 to x استفاده کنید بعد یک متغیر از نوع عددی (Integer) و یک متغیر از نوع boolean در سطح فرم بنویسید با عنوانی مثل Poused و وقتی روی دکمه مکث کلیک شد, متغیر عددی مزبور با شماره عضو پردازش شده برابر بشه و متغیر boolean رو برابر با 1 (درست) قرار بدید تا حلقه متوقف بشه
برای شروع مجدد, دوباره حلقه را از همان نقطه متوقف شده (که در متغیر عددی بالا ذخیره شده) تا انتهای فایل, ادامه می دهید و قبلش, متغیر بولین را برابر 0 می کنید.
تئوری خوبی بود!

alimanam
یک شنبه 19 آذر 1391, 11:01 صبح
با سلام


آیا میشه دکمه Pause و Resume به این برنامه اضافه کرد که وقتی داره کپی میکنه Pause رو بزنی کپی متوقف بشه و وقتی Resume رو میزنی کپی مجددا ادامه پیدا کنه /؟

برای این کار بهتره از نخ یا همان Threading استفاده بشه . که مثال برای این فضای نام علی ماشاا.. زیاده .


دوم اینکه اگر بخوام از thread یا backgroundworker هم استفاده کنم تا مثلا الان فرم قفل نکنه چه تغییراتی باید بدم ؟

لینک زیر بهتون نشون میده چطوری میشه این کار رو انجام بدین .

MTCopy: A Multi-threaded Single/Multi File Copying Tool (http://www.codeproject.com/Articles/24984/MTCopy-A-Multi-threaded-Single-Multi-File-Copying)


باید برای این کار, از حلقه For I=0 to x استفاده کنید بعد ...

بهتره از همان نخ استفاده بشه چون اینجوری عملکرد CPU هم یه سروسامانی میگیره .

موفق باشید./

ehsan-68
سه شنبه 21 آذر 1391, 10:24 صبح
با سلام

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


Imports System.IO

Public Class Form1

Private Sub btnSelectFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectFile.Click
Dim ofd As New OpenFileDialog With {.Filter = "All Files|*.*"}
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
txtFileDes.Text = ofd.FileName
txtFileSaveWhere.Text = ""
lblFileSize.Text = Math.Round(CInt(New FileInfo(ofd.FileName).Length.ToString) / 1024, 3) & " کیلو بایت "
End If
End Sub

Private Sub btnWhereToSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWhereToSave.Click
Dim sfd As New FolderBrowserDialog
If txtFileDes.Text.Length > 0 AndAlso sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
txtFileSaveWhere.Text = sfd.SelectedPath & "\" & New FileInfo(txtFileDes.Text).Name
End If
End Sub

Private Sub SaveBinaryFile(ByVal strFilename As String, ByVal bytesToWrite() As Byte)
Dim position As Integer = 0
Dim BufferSize As Integer = 4096

ProgressBar1.Value = 0
Using fsNew As FileStream = New FileStream(strFilename, FileMode.Create, FileAccess.Write)
Do
Dim intToCopy As Integer = Math.Min(BufferSize, bytesToWrite.Length - position)
Dim buffer(intToCopy - 1) As Byte
Array.Copy(bytesToWrite, position, buffer, 0, intToCopy)
fsNew.Write(buffer, 0, buffer.Length)
ProgressBar1.Value = ((position / bytesToWrite.Length) * 100)
lblAction.Text = "در حال کپی کردن فایل"
ProgressBar1.Refresh()
Application.DoEvents()
position += intToCopy
Loop While position < bytesToWrite.Length
ProgressBar1.Value = 0
lblAction.Text = ""
End Using
End Sub

Private Function ReadBinaryFile(ByVal strFilename As String) As Byte()
Dim position As Integer = 0
Dim bufferSize As Integer = 4096
Dim bytes() As Byte

ProgressBar1.Value = 0
Using fsOpen As FileStream = New FileStream(strFilename, FileMode.Open)
ReDim bytes((fsOpen.Length) - 1)
Do
If (position + bufferSize) > fsOpen.Length Then
fsOpen.Read(bytes, position, fsOpen.Length - position)
Exit Do
Else
fsOpen.Read(bytes, position, bufferSize)
End If
ProgressBar1.Value = ((position / fsOpen.Length) * 100)
lblAction.Text = "درحال خواندن فایل ..."
ProgressBar1.Refresh()
Application.DoEvents()
position += bufferSize
Loop
ProgressBar1.Value = 0
lblAction.Text = "آماده کپی کردن فایل"
End Using
Return bytes
End Function

Private Sub btnCopyFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopyFile.Click
If txtFileDes.Text.Length > 0 AndAlso txtFileSaveWhere.Text.Length > 0 Then
SaveBinaryFile(txtFileSaveWhere.Text, ReadBinaryFile(txtFileDes.Text))
End If
End Sub

End Class



موفق باشید./

میشه این دستورو طوری تغییر داد تا یک فولدر را با تمام فایلهایی که توشه کپی کرد؟

چجوری؟