文件的拷贝

 

/// <summary>
        
/// 文件拷贝
        
/// </summary>
        
/// <param name="from"></param>
        
/// <param name="to"></param>
        
/// <param name="timelength"></param>
        public void FileCopy(string from,string to,int timelength)
        {
            FileStream fromfile 
= new FileStream(from, FileMode.Open, FileAccess.Read);
            FileStream tofile 
= new FileStream(to, FileMode.Create, FileAccess.Write);
            
long length = fromfile.Length;
            
if(length<timelength)
            {
                
byte[] buff = new byte[length];
                fromfile.Read(buff, 
0, (int)length);
                fromfile.Flush();
                tofile.Write(buff, 
0, (int)length);
                tofile.Flush();
                label1.Text 
= "复制完成!";
                
this.Refresh();
                fromfile.Dispose();
                tofile.Dispose();
            }
            
else
            {
                
long copied = 0;
                
byte[] buff = new byte[timelength];
                
while(copied<=length)
                {
                    fromfile.Read(buff, 
0, timelength);
                    fromfile.Flush();
                    tofile.Write(buff, 
0, timelength);
                    tofile.Flush();
                    label1.Text 
= "已复制: " + copied + "/" + length;
                    copied 
+= timelength;
                    
this.Refresh();
                }
                
if(copied==length)
                {
                    label1.Text 
= "复制完成!";
                    
this.Refresh();
                }
else if(copied>length)
                {
                    
int temp = (int)(timelength - (copied - length));
                    
byte[] buf = new byte[temp];
                    fromfile.Read(buf, 
0, temp);
                    fromfile.Flush();
                    tofile.Write(buf, 
0, temp);
                    tofile.Flush();
                    label1.Text 
= "复制完成!";
                    
this.Refresh();
                }

                fromfile.Dispose();
                tofile.Dispose();

            }
        }

 

           

posted @ 2009-12-18 09:49  wingzhang  阅读(238)  评论(0)    收藏  举报