NickLi

导航

请使用C#的文件流来拷贝文件

用File.Copy拷贝了一个Excel文件,打开后发现丢掉了许多sheets.

之后发现用文件流来拷贝还是比较靠谱的, 代码很简单:

private static void TestFileCopy(string sourcePath, string destiPath)        

{            

FileStream fs = new FileStream(sourcePath, FileMode.Open, FileAccess.Read, FileShare.Read, 1024, false);           System.IO.BinaryReader br = new System.IO.BinaryReader(fs, System.Text.Encoding.Default);            

byte[] str = new byte[br.BaseStream.Length];          

br.Read(str, 0, str.Length);            

br.Close();            

fs.Close();
fs = new FileStream(destiPath, FileMode.Create);            

System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs, System.Text.Encoding.Default);            

bw.Write(str);            

bw.Flush();  // Important!!   

bw.Close();            

fs.Close();      

}

posted on 2011-03-25 10:34  孤叶无眠  阅读(583)  评论(0编辑  收藏  举报