首页 > 其他 > 详细

C#文件操作

时间:2014-08-19 18:22:15      阅读:300      评论:0      收藏:0      [点我收藏+]

1、判断指定路径下文件是否存在:

     /// <summary>
        /// 判断服务器上指定文件夹下指定文件是否存在。
        /// </summary>
        /// <returns>是否存在</returns>
        [WebMethod(Description = "判断指定路径下的指定文件是否存在。")]
        public bool IsScheduleFileExists(string serverFilePath,string serverFileName)
        {
            if (String.IsNullOrEmpty(serverFilePath))
            {
                serverFilePath = Server.MapPath("~/Temp");
            }

            if (!serverFilePath.EndsWith(@"\"))
            {
                serverFilePath += @"\";
            }

            if (!Directory.Exists(serverFilePath))
            {
                return false;
            }

            String destinationFileFullName = String.Format(@"{0}{1}", serverFilePath, serverFileName);
            if (System.IO.File.Exists(destinationFileFullName))
            {
                return true;
            }
            return false;
        }

2、合并指定路径下的文件集:

     /// <summary>
        /// 合并文件。
        /// </summary>
        /// <param name="packedCount">总包数。</param>
        /// <param name="serverFileSavePath">文件保存路径。</param>
        /// <param name="destinationFileFullName">文件全名。</param>
        private void MergeFiles(Int32 packedCount, String serverFileSavePath, String destinationFileFullName)
        {
            FileStream fs = new FileStream(destinationFileFullName, FileMode.Append, FileAccess.Write);
            FileStream fsTemp = null;
            String tempFileName = String.Empty;
            Int32 readBytes;
            Byte[] bytes = new Byte[this.bufferSize];
            for (Int32 idx = 1; idx < packedCount; idx++)  //从第二个线程接收的临时文件开始合并
            {
                tempFileName = String.Format(@"{0}{1}{2}.tmp", serverFileSavePath, idx);
                fsTemp = new FileStream(tempFileName, FileMode.Open, FileAccess.Read);
                readBytes = fsTemp.Read(bytes, 0, this.bufferSize);

                while (readBytes > 0)
                {
                    fs.Write(bytes, 0, readBytes);
                    readBytes = fsTemp.Read(bytes, 0, this.bufferSize);
                }

                fsTemp.Close();
                fsTemp.Dispose();
                File.Delete(tempFileName);
            }

            fs.Flush();
            fs.Close();
            fs.Dispose();
        }

  

 

C#文件操作,布布扣,bubuko.com

C#文件操作

原文:http://www.cnblogs.com/shenchao/p/3922489.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!