• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#文件操作

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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();
        }

  

 3、文件拷贝:

            FileInfo sourceFileInfo = new FileInfo(sourceFullFileName);
            FileInfo destFullFileInfo = new FileInfo(destFullFileName);
            if (sourceFileInfo.Length != destFullFileInfo.Length)
            {
                return "文件大小不一致";
            }    


            File.Delete(sourceFullFileName);    

 

 4、判断两文件大小:

                //先把目录创建出来, 否者Copy会失败
		string strPath = System.IO.Path.GetDirectoryName(destFullFileName);
		System.IO.Directory.CreateDirectory(strPath);

                File.Copy(sourceFullFileName, destFullFileName, true);        

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[转] C#开源资源(收集)发布时间:2022-07-10
下一篇:
C# 生成 PDF发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap