stream 文件操作

简单的帮助类:

        private static byte[] StreamToBytes(Stream fs)
        {
            byte[] bArr = new byte[fs.Length];
            fs.Read(bArr, 0, (int)fs.Length);
            fs.Seek(0, SeekOrigin.Begin);
            return bArr;
        }

        public static Stream BytesToStream(byte[] bytes)
        {
            Stream stream = new MemoryStream(bytes);
            return stream;
        }


        public static byte[] FileToBytes(string path)
        {
            using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                byte[] bArr = new byte[fileStream.Length];
                fileStream.Read(bArr, 0, bArr.Length);
                return bArr;
            }
        }

        public static string BytesToFile(string directoryPath,byte[] bArr,string fileName)
        {
            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }
            string path = directoryPath + "\\" + fileName;
            using (FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
            {
                fileStream.Seek(0, SeekOrigin.Begin);
                fileStream.Write(bArr, 0, bArr.Length);
            }
            return path;
        }

 

posted @ 2017-09-11 15:24  Roman-li  阅读(229)  评论(0编辑  收藏  举报