這篇文章主要介紹了C#自適應合并文件的方法,涉及C#基于FileStream類實現文件讀寫操作的相關技巧,非常簡單實用,需要的朋友可以參考下
本文實例講述了C#自適應合并文件的方法。分享給大家供大家參考。具體實現方法如下:
- using System;
- using System.IO;
- namespace MergeFile
- {
- class Program
- {
- public static void Main(string[] args)
- {
- int count=1;
- string sourcepath=@"D:/SplitFile";
- string filetomerge=@"C:/編程的奧秘.pdf";
- FileStream ftm = new FileStream(filetomerge, FileMode.Create, FileAccess.Write);
- BinaryWriter bw=new BinaryWriter(ftm);
- string filepath;
- while(File.Exists(filepath=sourcepath+Path.DirectorySeparatorChar+Path.GetFileName(filetomerge)+count++))
- {
- FileStream fsr = new FileStream(filepath, FileMode.Open, FileAccess.Read);
- BinaryReader br=new BinaryReader(fsr);
- bw.Write(br.ReadBytes((int)fsr.Length));
- br.Close();
- fsr.Close();
- }
- bw.Flush();
- bw.Close();
- ftm.Close();
- }
- }
- }
希望本文所述對大家的C#程序設計有所幫助。
新聞熱點
疑難解答