将目录下的图片文件写成一个二进制大文件

这个问题弄得我昨晚都没吃饭,终于搞定了!

DirectoryInfo di = new DirectoryInfo(folderallpath);//folderallpath是目录名  
FileInfo fiobj = new FileInfo(folderallpath + "\\total.obj"); //  total.obj就是二进制大文件
FileStream fsobj = fiobj.Open(FileMode.Append,FileAccess.Write); //这里应该用FileMode.Append,循环写入的时候,原有文件才不会被覆盖
foreach (FileInfo fi in di.GetFiles("*.*"))  
            {  
                if (fi.Extension.ToLower() == ".jpg"  ¦ ¦ fi.Extension.ToLower() == ".png"  ¦ ¦ fi.Extension.ToLower() == ".tif")  
                {   
                    string path = folderallpath + "/" + fi.Name;
                    int length = Int32.Parse(fi.Length.ToString());  

                    if (length != 0)  
                    {  
                        Byte[] FileByteArray = new Byte[length];
                        FileStream fspic = new FileStream(path, FileMode.Open, FileAccess.Read); 
                        fspic.Read(FileByteArray, 0, System.Convert.ToInt32(fspic.Length));
                        fspic.Flush();  
                        fsobj.Write(FileByteArray,0,length);  
                    }  
                }  
            }  
fsobj.Flush();  
fsobj.Close();

大功告成~~
posted on 2008-03-12 11:08  RedSoft  阅读(519)  评论(1编辑  收藏  举报