PDA

View Full Version : سوال: نمایش میزان کپی شدن فایل با Progessbar



ali190
شنبه 05 آذر 1390, 22:00 عصر
باسلام
دوستان من چطور میتونم عملیات کپی شدن یک فایل از مبداء به مقصد رو با Progessbar نمایش بدم
اگر سورسی در این زمینه در اختیارم قرار بدید ممنون میشم
ممنون
یاعلی

محمد فدوی
دوشنبه 07 آذر 1390, 18:45 عصر
چندین راه برای این کار هست:
1- استفاده از ریسمان
2- استفاده از تابع CopyFileEx (یک تابع API)
3- و...

ali190
دوشنبه 07 آذر 1390, 20:36 عصر
سلام
ممنون از راهنماییت
ولی من به سورس یا نمونه کد نیازمندم
ممنون میشم کمکم کنید
یاعلی

arash020
دوشنبه 07 آذر 1390, 23:44 عصر
سلام
از دست من فعلا همین براومد .


Here's the code for the first button - OpenFileDialog:


FileFromDialog.InitialDirectory = "c:\"
FileFromDialog.Filter = _
"txt files (*.txt)|*.txt|All files (*.*)|*.*"
dlgResult = FileFromDialog.ShowDialog()
If dlgResult <> System.Windows.Forms.DialogResult.OK Then
MessageBox.Show("File Error " & dlgResult)
End If
You have probably seen the OpenFileDialog in a thousand other applications that you use. The OpenFileDialog is powerful enough to check for the existance of files and folders, look for ReadOnly files, and even look for "multidotted extensions". (Which means just what it looks like. A file like "FileName.ext1.ext2.ext3". Yes, they do exist.)

In the case above, I specify that .txt or all files can be shown. You can set file filter default to any of of the listed file filter options using the FilterIndex property. It defaults to 1, the first filter in the list. The If statement is just a very simple check to see if a file was successfully selected.

The code for the second button - FolderBrowserDialog - is very similar:


dlgResult = FolderToDialog.ShowDialog()
If dlgResult <> System.Windows.Forms.DialogResult.OK Then
MessageBox.Show("Folder Error " & dlgResult)
End If
And the code for the third button actually copies the file using the very handy My.Computer.FileSystem.CopyFile object. I've continued this onto five lines to keep the lines short but it's just one statement:


My.Computer.FileSystem.CopyFile( _
FileFromDialog.FileName, _
FolderToDialog.SelectedPath & "\" & _
FileFromDialog.SafeFileName, _
FileIO.UIOption.AllDialogs)
There are really only two things of note here. The first is that I had to build the output path using the path selected with FolderBrowserDialog and the name selected with OpenFileDialog. The FileName property of OpenFileDialog has the path in it. SafeFileName just has the name. But the second is the key to the reader's question. FileIO.UIOption.AllDialogs shows a progress bar automatically!


موفق باشی

ali190
سه شنبه 08 آذر 1390, 18:32 عصر
سلام
ولی در این کد که از پروسزبار استفاده نشده!؟

mehdi.mousavi
سه شنبه 08 آذر 1390, 19:07 عصر
سلام.
برای اینکه Progress Bar رو هنگام عمل Copy، Move و Delete نشون بدید، باید از امکانات Shell استفاده کنید. این مقاله (http://www.codeproject.com/KB/shell/csdoesshell2.aspx)، به شما چگونگی انجام این کار رو در C#‎ نشون میده (بدین ترتیب، هنگام Copy کردن فایل، پنجره استاندارد Copy باز میشه و کاربر Animation مربوطه رو میبینه)... تبدیل کدهای C#‎ به VB.NET نیز با ابزارهای Online بسادگی میسره (حتی VS 2011 این امکان رو بصورت Built-in داره)!

روش دیگر، استفاده از CopyFileEx (http://www.pinvoke.net/default.aspx/kernel32.CopyFileEx) هستش که بر خلاف CopyFile، روند پیشرفت Copy رو گزارش میده.

موفق باشید.

ali190
چهارشنبه 09 آذر 1390, 23:34 عصر
سلام
ممنون از راهنماییهاتون
ولی من شدیداً به دنبال یک سورس در این زمینه هستم
ممنون میشم که در اختیارم برارید
خیلی خیلی ممنونم
یاعلی

alimanam
پنج شنبه 10 آذر 1390, 18:37 عصر
با سلام

روش که علی ماشاا.. زیاده این هم یک روش :


Imports System.IO
Public Class Form1

Public Sub CopyFileForAli190()
Dim ofd As New OpenFileDialog
ofd.Filter = "All files|*.*"
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim oFolderDlg As New FolderBrowserDialog
If oFolderDlg.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim fi As New IO.FileInfo(ofd.FileName)
Dim sr As New IO.FileStream(ofd.FileName, IO.FileMode.Open)

Dim sw As New IO.FileStream(oFolderDlg.SelectedPath & "\" & Path.GetFileName(ofd.FileName), IO.FileMode.Create)
Dim len As Long = sr.Length - 1
For i As Long = 0 To len
sw.WriteByte(sr.ReadByte)
If i Mod 1000 = 0 Then
ProgressBar1.Value = i * 100 / len
Application.DoEvents()
End If
Next
sr.Close() : sw.Close()
ProgressBar1.Value = 0
End If
End If

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
CopyFileForAli190() ' !!!
End Sub

End Class


موفق باشید ./

srezay
جمعه 28 شهریور 1393, 10:12 صبح
واسه درصد نخوایم خیلی پیچیده کنیم مسئله رو میشه توی یه تایمر
label1.text=progressbar1.value & " %" رو بذاریم :لبخند: