网站导航网学 原创论文 原创专题 网站设计 最新系统 原创论文 论文降重 发表论文 论文发表 UI设计定制 论文答辩PPT格式排版 期刊发表 论文专题
返回网学首页
网学原创论文
最新论文 推荐专题 热门论文 论文专题
当前位置: 网学 > 设计资源 > .Net编程 > 正文

C#在进度条中显示复制文件的进度

论文降重修改服务、格式排版等 获取论文 论文降重及排版 论文发表 相关服务
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.ComponentModel; 
  4. using System.Data; 
  5. using System.Drawing; 
  6. using System.Linq; 
  7. using System.Text; 
  8. using System.Windows.Forms; 
  9. using System.IO; 
  10. using System.Xml; 
  11. using System.Collections; 
  12. using System.Reflection; 
  13. using System.Threading; 
  14.  
  15. namespace CopyForm 
  16.     public partial class frmMain : Form 
  17.     { 
  18.         int totalSize; 
  19.         int position; 
  20.         const int BUFFER_SIZE = 4096; 
  21.         byte buffer; 
  22.         Stream stream; 
  23.  
  24.         public frmMain() 
  25.         { 
  26.             InitializeComponent(); 
  27.         } 
  28.  
  29.         private void frmMain_Load(object sender, EventArgs e) 
  30.         { 
  31.         } 
  32.  
  33.         private void btnCopy_Click(object sender, EventArgs e) 
  34.         { 
  35.             string strFile = ""
  36.             OpenFileDialog dlg = new OpenFileDialog(); 
  37.             if (dlg.ShowDialog() == DialogResult.OK) 
  38.             { 
  39.                 strFile = dlg.FileName; 
  40.             } 
  41.             else 
  42.             { 
  43.                 return
  44.             } 
  45.  
  46.             FileStream fs = new FileStream(strFile, FileMode.Open, FileAccess.Read); 
  47.             totalSize = (int)fs.Length; 
  48.             stream = fs; 
  49.  
  50.             //Delete file which aready exists. 
  51.             if (File.Exists("c:\\CopyFile.bin")) 
  52.             { 
  53.                 File.Delete("c:\\CopyFile.bin"); 
  54.             } 
  55.  
  56.             //Copy file while larger than 4KB. 
  57.             if (totalSize > BUFFER_SIZE) 
  58.             { 
  59.                 buffer = new byte[BUFFER_SIZE]; 
  60.  
  61.                 // Async Invoke 
  62.                 stream.BeginRead(buffer, 0, BUFFER_SIZE, new AsyncCallback(AsyncCopyFile), null); 
  63.                 MessageBox.Show("文件拷贝完毕!"); 
  64.             } 
  65.             else 
  66.             { 
  67.                 fs.Close(); 
  68.             } 
  69.         } 
  70.  
  71.         /// <summary> 
  72.         /// Asynchronously copy file 
  73.         /// </summary> 
  74.         /// <param name="ar"></param> 
  75.         private void AsyncCopyFile(IAsyncResult ar) 
  76.         { 
  77.             int readenLength; 
  78.  
  79.             //Lock FileStream 
  80.             lock (stream) 
  81.             { 
  82.                 // When stream endread, get readed length 
  83.                 readenLength = stream.EndRead(ar); 
  84.             } 
  85.  
  86.             // Write to disk 
  87.             FileStream fsWriter = new FileStream("c:\\CopyFile.bin", FileMode.Append, FileAccess.Write); 
  88.             fsWriter.Write(buffer, 0, buffer.Length); 
  89.             fsWriter.Close(); 
  90.  
  91.             // Current stream position  
  92.             position += readenLength; 
  93.          
  94.             // Response UI 
  95.             MethodInvoker m = new MethodInvoker(SynchProgressBar); 
  96.             m.BeginInvoke(nullnull); 
  97.  
  98.             if (position >= totalSize)  //read over 
  99.             { 
  100.                 stream.Close(); 
  101.                 return
  102.             } 
  103.  
  104.             // Continue to read and write 
  105.             lock (stream) 
  106.             { 
  107.                 int leftSize = totalSize - position; 
  108.  
  109.                 if (leftSize < BUFFER_SIZE) 
  110.                 { 
  111.                     buffer = new byte[leftSize]; 
  112.                 } 
  113.                 stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(AsyncCopyFile), null); 
  114.             } 
  115.         } 
  116.  
  117.         private void SynchProgressBar() 
  118.         { 
  119.             this.progressBar1.Minimum = 0; 
  120.             //this.progressBar1.Maximum = totalSize; 
  121.             //this.progressBar1.Value = position; 
  122.             SetControlPropertyValue(this.progressBar1, "Maximum", totalSize); 
  123.             SetControlPropertyValue(this.progressBar1, "Value", position); 
  124.         } 
  125.  
  126.         /// <summary> 
  127.         /// Cross-thread operation not valid: (solution key) 
  128.         /// Control 'progressBar1' accessed from a thread other than the thread it was created on 
  129.         /// </summary> 
  130.         /// <param name="oControl"></param> 
  131.         /// <param name="propName"></param> 
  132.         /// <param name="propValue"></param> 
  133.         delegate void SetControlValueCallback(Control oControl, string propName, object propValue); 
  134.         private void SetControlPropertyValue(Control oControl, string propName, object propValue) 
  135.         { 
  136.             if (oControl.InvokeRequired) 
  137.             { 
  138.                 SetControlValueCallback d = new SetControlValueCallback(SetControlPropertyValue); 
  139.                 oControl.Invoke(d, new object { oControl, propName, propValue }); 
  140.             } 
  141.             else 
  142.             { 
  143.                 Type t = oControl.GetType(); 
  144.                 PropertyInfo props = t.GetProperties(); 
  145.                 foreach (PropertyInfo p in props) 
  146.                 { 
  147.                     if (p.Name.ToUpper() == propName.ToUpper()) 
  148.                     { 
  149.                         p.SetValue(oControl, propValue, null); 
  150.                     } 
  151.                 } 
  152.             } 
  153.         } 
  154.  
  155.         private void btnExit_Click(object sender, EventArgs e) 
  156.         { 
  157.             Application.Exit(); 
  158.         } 
  159.  
  160.         private void frmMain_FormClosed(object sender, FormClosedEventArgs e) 
  161.         { 
  162.             GC.Collect(); 
  163.             GC.WaitForPendingFinalizers(); 
  164.         } 
  165.     } 
设为首页 | 加入收藏 | 网学首页 | 原创论文 | 计算机原创
版权所有 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
Copyright 2008-2020 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号 常年法律顾问:王律师