C#文件操作

转载 2011年01月19日 10:17:00

//复制一个目录 递归删除文件 和 文件夹

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Common.Files
{
    public class FileOperator
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="encoding">编码</param>
        /// <returns>文件内容</returns>
        public string ReadFile(string path, System.Text.Encoding encoding)
        {
            string tempContent = "";
            using (StreamReader sr = new StreamReader(path, encoding))
            {
                tempContent = sr.ReadToEnd();
            }
            return tempContent;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="path">文件的路径</param>
        /// <param name="append">是否追加</param>
        /// <param name="encoding">编码</param>
        /// <param name="content">内容</param>
        /// <returns>是否成功</returns>
        public bool WriteFile(string path,bool append,System.Text.Encoding encoding,string content)
        {
            bool state = false;
            try
            {
                using (StreamWriter sw = new StreamWriter(path, append, encoding))
                {
                    sw.Write(content);
                    sw.Flush();
                    state = true;
                }
            }
            catch
            {
 
            }
            return state;
        }
        /// <summary>
        /// 拷贝一个路径下的文件夹和文件
        /// </summary>
        /// <param name="olddirectory"></param>
        /// <param name="newdirectory"></param>
        /// <returns></returns>
        public bool CopyDirectoryAndFiles(string olddirectory, string newdirectory)
        {
            bool state = false;
            try
            {
                DirectoryInfo dis = new DirectoryInfo(newdirectory);
                if (!dis.Exists)
                {
                    Directory.CreateDirectory(newdirectory);
                }
                CopyAll(olddirectory, newdirectory, "");
                state = true;
            }
            catch
            {
 
            }
            return state;
        }
        /// <summary>
        /// 递归拷贝一个文件夹的所有文件
        /// </summary>
        /// <param name="olddirectory"></param>
        /// <param name="newdirectory"></param>
        /// <param name="dir"></param>
        protected void CopyAll(string olddirectory, string newdirectory, string dir)
        {
            DirectoryInfo dis = new DirectoryInfo(dir.Length > 0 ? (dir) : (olddirectory));
            FileInfo[] files = dis.GetFiles();
            foreach (FileInfo fi in files)
            {

                try
                {
                    //拷贝文件
                    File.Copy(fi.FullName, string.Format("{0}//{1}", dir.Length > 0 ? (dir.Replace(olddirectory, newdirectory)) : (newdirectory), fi.Name));

                }
                catch (Exception ex)
                {
                    try
                    {
                        File.Delete(string.Format("{0}//{1}", dir.Length > 0 ? (dir.Replace(olddirectory, newdirectory)) : (newdirectory), fi.Name));
                        //string temp = ex.Message;
                        File.Copy(fi.FullName, string.Format("{0}//{1}", dir.Length > 0 ? (dir.Replace(olddirectory, newdirectory)) : (newdirectory), fi.Name));
                    }
                    catch
                    {

                    }
                }


            }
            if (dis.GetDirectories().Length > 0)
            {
                for (int i = 0; i < dis.GetDirectories().Length; i++)
                {
                    //创建目录
                    string tempNewPath = string.Format("{0}//{1}", dir.Length > 0 ? (dir.Replace(olddirectory, newdirectory)) : (newdirectory), dis.GetDirectories()[i].Name);
                    if (!Directory.Exists(tempNewPath))
                        Directory.CreateDirectory(tempNewPath);
                    CopyAll(olddirectory, newdirectory, dis.GetDirectories()[i].FullName);
                }
            }


        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="oldFilePath"></param>
        /// <param name="newFilePath"></param>
        /// <returns></returns>
        public bool CopyFile(string oldFilePath, string newFilePath)
        {
            bool state = false;
            File.Copy(oldFilePath, newFilePath);
            return state;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="directory"></param>
        /// <returns></returns>
        public bool DelteDirectory(string directory)
        {
            bool state = false;
            DeleteDirectoryAndFiles(directory);
            return state;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="path"></param>
        protected void DeleteDirectoryAndFiles(string path)
        {
            DirectoryInfo di = new DirectoryInfo(path);
            if (di.GetFiles().Length > 0)
                DeleteFiles(path);
         
           DirectoryInfo[] dis = di.GetDirectories();
           if (dis.Length > 0)
           {
               foreach (DirectoryInfo tempdi in dis)
               {
               
                   DeleteDirectoryAndFiles(tempdi.FullName);
                   if (tempdi.GetFiles().Length < 1 && tempdi.GetDirectories().Length < 1)
                       tempdi.Delete();
               }
           }
        }
        /// <summary>
        /// 删除文件夹的所有文件
        /// </summary>
        /// <param name="path"></param>
        protected void DeleteFiles(string path)
        {
            DirectoryInfo di = new DirectoryInfo(path);
            FileInfo[] fis = di.GetFiles();
            foreach (FileInfo fi in fis)
            {
                try
                {
                    fi.Delete();
                }
                catch
                {
                    continue;
                }
            }
        }
    }
}

C#文件操作类.pdf

  • 2013年08月28日 15:33
  • 619KB
  • 下载

C# 文件操作

  • 2013年03月10日 13:08
  • 457KB
  • 下载

C#中的读写文件操作函数总结

fopen, fclose, fwrite是C/C++中,用于文件读取写入的函数。 在C#语言中,有很多方法可以进行文件读写。 下面是通过StreamReader,FileSt...

C#文件操作大全

  • 2012年12月07日 18:08
  • 30KB
  • 下载

C#文件操作

  • 2012年01月04日 10:47
  • 16KB
  • 下载

C# 文件操作类集合(一) 路径

常用的文件操作类的方法汇总: using System; using System.Text; using System.IO; namespace DotNet.Utilities { /...

C# ini文件操作

  • 2016年11月16日 10:08
  • 99KB
  • 下载

C#文件操作大全

  • 2011年09月26日 20:57
  • 66KB
  • 下载

C# XML文件操作类XmlHelper类的完整代码

C# XML文件操作类XmlHelper类的完整代码:   using System; using System.Collections; using System.Xml; namespace Co...

C#目录和ini文件操作

  • 2010年05月25日 12:30
  • 7KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:C#文件操作
举报原因:
原因补充:

(最多只允许输入30个字)