WPF 文件拷贝进度显示

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
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值