WPF 文件拷贝进度显示
2011-09-13 11:41
453 查看
public partial class MainWindow : Window { int totalSize; int position; const int BUFFER_SIZE = 1024*1024; byte[] buffer; Stream stream; public MainWindow() { InitializeComponent(); } string copypath = ""; private void btncopy_Click(object sender, RoutedEventArgs e) { System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog(); System.Windows.Interop.HwndSource source = PresentationSource.FromVisual(this) as System.Windows.Interop.HwndSource; System.Windows.Forms.IWin32Window win = new OldWindow(source.Handle); System.Windows.Forms.DialogResult result = dlg.ShowDialog(win); //File.Copy("", dlg.SelectedPath + @"/" + "", true); copypath = dlg.SelectedPath + @"/" + "拷贝文件.zip"; //string strFile = ""; //OpenFileDialog dlg = new OpenFileDialog(); //if (dlg.ShowDialog().ToString().Equals("OK")) //{ // strFile = dlg.FileName; //} //else //{ // return; //} FileStream fs = new FileStream(@"E:\Videos\传输测试文件.zip", FileMode.Open, FileAccess.Read); totalSize = (int)fs.Length; stream = fs; //Delete file which aready exists. //if (File.Exists("传输测试文件.zip")) //{ // File.Delete("传输测试文件.zip"); //} //Copy file while larger than 4KB. if (totalSize > BUFFER_SIZE) { buffer = new byte[BUFFER_SIZE]; // Async Invoke stream.BeginRead(buffer, 0, BUFFER_SIZE, new AsyncCallback(AsyncCopyFile), null); //MessageBox.Show("文件拷贝完毕!"); } else { fs.Close(); } } private void AsyncCopyFile(IAsyncResult ar) { int readenLength; //Lock FileStream lock (stream) { // When stream endread, get readed length readenLength = stream.EndRead(ar); } // Write to disk FileStream fsWriter = new FileStream(copypath, FileMode.Append, FileAccess.Write); fsWriter.Write(buffer, 0, buffer.Length); fsWriter.Close(); // Current stream position position += readenLength; // Response UI //MethodInvoker m = new MethodInvoker(SynchProgressBar); //m.BeginInvoke(null, null); Dispatcher.Invoke(new Action(SynchProgressBar)); if (position >= totalSize) //read over { stream.Close(); return; } // Continue to read and write lock (stream) { int leftSize = totalSize - position; if (leftSize < BUFFER_SIZE) { buffer = new byte[leftSize]; } stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(AsyncCopyFile), null); } } private void SynchProgressBar() { this.progressBar1.Minimum = 0; //this.progressBar1.Maximum = totalSize; //this.progressBar1.Value = position; SetControlPropertyValue(this.progressBar1, "Maximum", totalSize); SetControlPropertyValue(this.progressBar1, "Value", position); } delegate void SetControlValueCallback(Control oControl, string propName, object propValue); private void SetControlPropertyValue(Control oControl, string propName, object propValue) { if (System.Threading.Thread.CurrentThread!=oControl.Dispatcher.Thread) { SetControlValueCallback d = new SetControlValueCallback(SetControlPropertyValue); oControl.Dispatcher.Invoke(d, new object[] { oControl, propName, propValue }); } else { Type t = oControl.GetType(); PropertyInfo[] props = t.GetProperties(); foreach (PropertyInfo p in props) { if (p.Name.ToUpper() == propName.ToUpper()) { p.SetValue(oControl, propValue, null); } } } } private void btnexit_Click(object sender, RoutedEventArgs e) { Application.Current.MainWindow.Close(); } private void Window_Unloaded(object sender, RoutedEventArgs e) { GC.Collect(); GC.WaitForPendingFinalizers(); } } public class OldWindow : System.Windows.Forms.IWin32Window { IntPtr _handle; public OldWindow(IntPtr handle) { _handle = handle; } #region IWin32Window Members IntPtr System.Windows.Forms.IWin32Window.Handle { get { return _handle; } } #endregion }
相关文章推荐
- 拷贝文件的时候显示进度
- 拷贝文件(能显示进度条)[原创]
- 拷贝文件(能显示进度条)[原创]
- WPF调用线程(二)复制文件并显示进度条(2)
- C# 文件拷贝的进度显示
- 用java程序把本地的一个文件拷贝到hdfs上并显示进度
- SHFileOperation在拷贝、删除文件时显示运行进度(转)
- 带有进度显示的文件拷贝模块
- Linux C 实现mycp 可以显示文件拷贝进度
- 大文件拷贝,可以显示进度和耗时
- MFC拷贝文件及进度条显示
- MFC拷贝文件及进度条显示
- WPF调用线程(二)复制文件并显示进度条
- 拷贝大文件 显示进度条 c# 异步线程
- 文件拷贝时显示进度条
- WPF 循环读取文件中实现进度条显示
- wpf文件复制显示进度条效果(双线程)
- C#利用VB的My功能来显示文件拷贝、删除、移动进度
- 大文件拷贝,可以显示进度和耗时
- 计算文件夹大小、拷贝文件显示进度