C#类 文件操作类

using System;
using System.Collections.Generic;
using System.Text;
using System.IO ;
using System.Collections;

namespace zxf
{
    public class FK_FILE
    {
        private static ArrayList AllFolder = new ArrayList();
        private static ArrayList AllFile = new ArrayList();

        /// <summary>
        /// 读取一个UTF16的文件
        /// </summary>
        /// <param name="FilePath">源文件地址</param>
        public  static string RD_UTF16(string FilePath)
        {
            StreamReader din = new StreamReader(FilePath, Encoding.Unicode);
            string txt = din.ReadToEnd();
            din.Close();
            return txt;
        }
        /// <summary>
        /// 读取一个GB2312的文件
        /// </summary>
        /// <param name="FilePath">源文件地址</param>
        private static string RD_GB2312(string FilePath)
        {
            StreamReader din = new StreamReader(FilePath, Encoding.GetEncoding("GB2312"));
            string txt = din.ReadToEnd();
            din.Close();
            return txt;
        }
        /// <summary>
        /// 写一个UTF16的文件
        /// </summary>
        /// <param name="FilePath">新文件地址</param>
        /// /// <param name="txt">新文件内容</param>
        private static bool WR_UTF16(string FilePath, string txt)
        {
            try
            {
                FileStream wr;
                File.Delete(FilePath);
                wr = File.OpenWrite(FilePath);
                StreamWriter bw = new StreamWriter(wr, Encoding.Unicode);
                bw.Write(txt);
                //清空缓冲区
                bw.Flush();
                //关闭流
                bw.Close();
                wr.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 写一个GB2312的文件
        /// </summary>
        /// <param name="FilePath">新文件地址</param>
        /// /// <param name="txt">新文件内容</param>
        private static bool WR_GB2312(string FilePath, string txt)
        {
            try
            {
                FileStream wr;
                File.Delete(FilePath);
                wr = File.OpenWrite(FilePath);
                StreamWriter bw = new StreamWriter(wr, Encoding.GetEncoding("GB2312"));
                bw.Write(txt);
                //清空缓冲区
                bw.Flush();
                //关闭流
                bw.Close();
                wr.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 复制一个文件到新的地址
        /// </summary>
        /// <param name="OldFilePath">源文件地址</param>
        /// <param name="NewFilePath">新文件地址</param>
        private static bool Copy(string OldFilePath, string NewFilePath)
        {
            try
            {

                File.Copy(OldFilePath, NewFilePath);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// Copy一个文件夹及其下面的所有文件方法1
        /// </summary>
        /// <param name="OldFolderFile">源文件地址</param>
        /// <param name="NewFolderFile">新文件地址</param>
        public static bool CopyFolderFile(string OldFolderFile, string NewFolderFile)
        {
            String[] Files;

            if (NewFolderFile[NewFolderFile.Length - 1] != Path.DirectorySeparatorChar) NewFolderFile += Path.DirectorySeparatorChar;
            if (!Directory.Exists(NewFolderFile)) Directory.CreateDirectory(NewFolderFile);
            Files = Directory.GetFileSystemEntries(OldFolderFile);
            foreach (string Element in Files)
            {
                if (Directory.Exists(Element))
                    CopyFolderFile(Element, NewFolderFile + Path.GetFileName(Element));
                else
                {
                    try
                    {
                        System.IO.File.Copy(Element, NewFolderFile + Path.GetFileName(Element), true);
                    }
                    catch
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// 移动一个文件
        /// </summary>
        /// <param name="FilePath">旧文件地址</param>
        /// /// <param name="FilePath">新文件地址</param>
             private static bool Move(string OldFilePath, string NewFilePath)
             {
                 try
                 {

                     File.Move(OldFilePath, NewFilePath);
                     return true;
                 }
                 catch
                 {
                     return false;
                 }
             }

             /// <summary>
             /// 删除一个文件
             /// </summary>
             /// <param name="FilePath">源文件地址</param>
             private static bool Delete(string FilePath)
             {
                 try
                 {
                     File.Delete(FilePath);
                     return true;
                 }
                 catch
                 {
                     return false;
                 }
             }

             /// <summary>
             /// 创建一个文件夹包括子目录
             /// </summary>
             /// <param name="FolderPath">源文件地址</param>
             private static bool CreateFolder(string FolderPath)
             {

                 bool bexistfile = false;
                 if (File.Exists(FolderPath))
                 {
                     bexistfile = true;
                 }
                 else //判断路径中的文件夹是否存在
                 {
                     string dirpath = FolderPath.Substring(0, FolderPath.LastIndexOf('\\'));
                     string[] pathes = dirpath.Split('\\');
                     if (pathes.Length > 1)
                     {
                         string path = pathes[0];
                         for (int i = 1; i < pathes.Length; i++)
                         {
                             path += "\\" + pathes[i];
                             if (!Directory.Exists(path))
                             {
                                 Directory.CreateDirectory(path);
                             }
                         }
                     }
                 }
                 return bexistfile;
             }

             /// <summary>
             /// 删除一个文件夹下所有文件
             /// </summary>
             /// <param name="FolderPath">文件夹路径</param>
             private static bool DeleteFolder(string FolderPath)
             {
                 if (Directory.Exists(FolderPath))           //如果存在这个文件夹,执行删除操作
                 {
                     foreach (string d in Directory.GetFileSystemEntries(FolderPath))
                     {
                         if (File.Exists(d))
                         {
                             try
                             {
                                 File.Delete(d);
                                 
                             }
                             catch
                             {
                                 return false;
                             }
                             //直接删除其中的文件
                         }
                         else
                         {
                             //递归删除子文件夹
                             if (DeleteFolder(d))
                             {
                                 return true;
                             }
                             else
                             {
                                 return false;
                             }
                         }
                     }
                     //删除已空文件夹
                     try
                     {
                         Directory.Delete(FolderPath, true);
                         return true;
                     }
                     catch
                     {
                         return false;
                     }
                 }
                     return true;
             }

             /// <summary>
             /// Copy一个文件夹及其下面的所有文件方法2
             /// </summary>
             /// <param name="OldFolderFile">源文件地址</param>
             /// <param name="NewFolderFile">新文件地址</param>
             public static bool CopyFolder(string OldFolderFile, string NewFolderFile)
             {
                 if (!Directory.Exists(OldFolderFile))
                     return false;

                 if (!Directory.Exists(NewFolderFile))
                 {
                     Directory.CreateDirectory(NewFolderFile);
                 }

                 string[] files = Directory.GetFiles(OldFolderFile);
                 foreach (string formFileName in files)
                 {
                     string fileName = Path.GetFileName(formFileName);
                     string toFileName = Path.Combine(NewFolderFile, fileName);
                     try
                     {
                         File.Copy(formFileName, toFileName);
                     }
                     catch
                     {
                         return false;
                     }
                 }
                 string[] fromDirs = Directory.GetDirectories(OldFolderFile);
                 foreach (string fromDirName in fromDirs)
                 {
                     string dirName = Path.GetFileName(fromDirName);
                     string toDirName = Path.Combine(NewFolderFile, dirName);
                     CopyFolder(fromDirName, toDirName);
                 }
                 return true;
             }

             /// <summary>
             /// 移动文件夹
             /// </summary>
             /// <param name="FilePath">源文件地址</param>
             /// <param name="FilePath">源文件地址</param>
             public static bool MoveFolder(string OldFolderPath, string NewFolderPath)
             {
                 if (!Directory.Exists(OldFolderPath))
                     return false;
                 if (CopyFolder(OldFolderPath, NewFolderPath))
                 {
                 }
                 else
                 {
                     return false;
                 }
                 try
                 {
                     Directory.Delete(OldFolderPath, true);
                 }
                 catch
                 {
                     return false;
                 }
                 return true;
             }

             //私有的,获得所有子目录
             private static bool GetAllDirList(string FolderPath)
             {
                 DirectoryInfo di = new DirectoryInfo(FolderPath);
                 DirectoryInfo[] diA = di.GetDirectories();
                 for (int i = 0; i < diA.Length; i++)
                 {
                     AllFolder.Add(diA[i].FullName);
                     //diA[i].FullName是某个子目录的绝对地址,把它记录在ArrayList中
                     GetAllDirList(diA[i].FullName);
                     //注意:递归了。逻辑思维正常的人应该能反应过来
                 }
                 return true;
             }

             /// <summary>
             /// 获得文件夹下目录中的所有文件
             /// </summary>
             /// <param name="FolderPath">源文件地址</param>
             public static ArrayList GetAllFileList(string FolderPath)
             {
                 AllFolder.Clear();
                 ArrayList Files = new ArrayList();
                 GetAllDirList(FolderPath);
                 foreach (string Folder in AllFolder)
                 {
                     Files.Add(GetFolderFile(Folder));
                 }
                 return Files;
             }
             /// <summary>
             /// 获得文件夹下所有子目录
             /// </summary>
             /// <param name="FolderPath">源文件地址</param>
             public static ArrayList GetFolderFile(string FolderPath)
             {
                 ArrayList Files = new ArrayList();
                 DirectoryInfo dir = new DirectoryInfo(FolderPath); ;
                 FileInfo[] files = dir.GetFiles();
                 foreach (FileInfo f in files)
                 {
                     string fileName = f.Name;
                     AllFile.Add(fileName);
                 }
                 return Files ;
             }


        //结束

    }


}
 

猜你喜欢

转载自blog.csdn.net/fkzxf/article/details/105511381