国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#文件分割的方法

2019-10-29 21:41:37
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了C#文件分割的方法,針對小于等于64M文件和大于64M文件兩種情況分析了C#文件分割的實現技巧,需要的朋友可以參考下

本文實例講述了C#文件分割的方法。分享給大家供大家參考。具體如下:

1. 小文件分割(適用于小于等于64M的文件):

 

 
  1. using System; 
  2. using System.IO; 
  3. string filetosplit=@"C:/temp/data.bin"
  4. string targetpath=@"D:/store"
  5. FileStream fsr = new FileStream(filetosplit, FileMode.Open, FileAccess.Read); 
  6. long FileLength=fsr.Length; 
  7. byte[] btArr = new byte[FileLength]; 
  8. fsr.Read(btArr, 0, (int)FileLength); 
  9. fsr.Close(); 
  10. int splitcount=3; 
  11. long PartLength=FileLength/splitcount+FileLength%splitcount; 
  12. int nCount=(int)Math.Ceiling((double)FileLength/PartLength); 
  13. string strFileName=Path.GetFileName(filetosplit); 
  14. long byteCount=0; 
  15. for(int i=1;i<=nCount;i++,byteCount=(i<nCount?byteCount+PartLength:FileLength-PartLength)) 
  16. FileStream fsw = new FileStream(targetpath + Path.DirectorySeparatorChar+ strFileName +i, FileMode.Create, FileAccess.Write); 
  17. fsw.Write(btArr, (int)byteCount, (int)(i<nCount?PartLength:FileLength-byteCount)); 
  18. fsw.Flush(); 
  19. fsw.Close(); 

2. 大文件分割(適用于大于64M的文件)

 

 
  1. using System; 
  2. using System.IO 
  3. string filetosplit=@"C:/temp/data.bin"
  4. string targetpath=@"D:/store"
  5. FileStream fsr = new FileStream(filetosplit, FileMode.Open, FileAccess.Read); 
  6. long FileLength=fsr.Length; 
  7. byte[] btArr = new byte[FileLength]; 
  8. fsr.Read(btArr, 0, (int)FileLength); 
  9. fsr.Close(); 
  10. int splitcount=3; 
  11. long PartLength=FileLength/splitcount+FileLength%splitcount; 
  12. int nCount=(int)Math.Ceiling((double)FileLength/PartLength); 
  13. string strFileName=Path.GetFileName(filetosplit); 
  14. long byteCount=0; 
  15. for(int i=1;i<=nCount;i++,byteCount=(i<nCount?byteCount+PartLength:FileLength-PartLength)) 
  16. FileStream fsw = new FileStream(targetpath + Path.DirectorySeparatorChar+ strFileName +i, FileMode.Create, FileAccess.Write); 
  17. long bc=byteCount; 
  18. long PartCount=i<nCount?PartLength:FileLength-bc; 
  19. int PartBufferCount=(int)(PartCount<int.MaxValue/32?PartCount:int.MaxValue/32); 
  20. int nc=(int)Math.Ceiling((double)PartCount/PartBufferCount); 
  21. for(int j=1;j<=nc;j++,bc=(j<nCount?bc+PartBufferCount:PartCount-PartBufferCount)) 
  22. fsw.Write(btArr, (int)bc, (int)(j<nc?PartBufferCount:PartCount-bc)); 
  23. fsw.Flush(); 
  24. fsw.Close(); 
  25. fsr.Close(); 

希望本文所述對大家的C#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 邓州市| 麦盖提县| 唐河县| 余江县| 陆丰市| 武宣县| 炎陵县| 额敏县| 拉萨市| 丰原市| 会宁县| 龙海市| 抚宁县| 平泉县| 德清县| 鹿邑县| 东源县| 锡林浩特市| 宁南县| 江达县| 阜宁县| 霍林郭勒市| 清河县| 江达县| 静海县| 高陵县| 高邑县| 探索| 获嘉县| 庆城县| 丁青县| 财经| 沧州市| 尚义县| 康保县| 彝良县| 高阳县| 彭泽县| 忻城县| 平江县| 建阳市|