当前位置: 首页 > article >正文

文件工具类 - C#小函数类推荐

       此文记录的是文件工具类。

/***

    文件工具类

    Austin Liu 刘恒辉
    Project Manager and Software Designer

    E-Mail: lzhdim@163.com
    Blog:   http://lzhdim.cnblogs.com
    Date:   2024-01-15 15:18:00

***/

namespace Lzhdim.LPF.Utility
{
    using System.Collections;
    using System.IO;

    /// <summary>
    /// The Object End of File
    /// </summary>
    public static class FileUtil
    {
        /// <summary>
        /// Append content to a file
        /// </summary>
        /// <param name="file">file path,include dir and file name</param>
        /// <param name="content">file content which to append</param>
        /// <returns>true append sucess;false append error</returns>
        public static bool Append2File(string file, string content)
        {
            if (!File.Exists(file))
            {
                string dir = System.IO.Path.GetDirectoryName(file);

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

                try
                {
                    FileStream fs = File.Create(file);
                    fs.Close();
                    fs.Dispose();
                }
                catch
                { return false; }
            }

            try
            {
                StreamWriter sw = new StreamWriter(file, true);
                sw.Write(content);
                sw.Close();
                sw.Dispose();

                return true;
            }
            catch
            { }

            return false;
        }

        /// <summary>
        /// copy a file
        /// </summary>
        /// <param name="filePathName">source file</param>
        /// <param name="toFilesPath">target file</param>
        public static void CopyFile(string filePathName, string toFilesPath)
        {
            FileInfo file = new FileInfo(filePathName);
            file.CopyTo(toFilesPath, true);
        }

        /// <summary>
        /// Create a file
        /// </summary>
        /// <param name="file">file path,include dir and file name</param>
        /// <returns>true Create Done;false Create error</returns>
        public static bool CreateFile(string file)
        {
            if (!File.Exists(file))
            {
                string dir = System.IO.Path.GetDirectoryName(file);

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

                try
                {
                    FileStream fs = File.Create(file);
                    fs.Close();
                    fs.Dispose();

                    return true;
                }
                catch
                { }

                return false;
            }

            return true;
        }

        /// <summary>
        /// delete a file
        /// </summary>
        /// <param name="file">dir which need to search</param>
        /// <returns>true delete sucess;false delete error</returns>
        public static bool DeleteFile(string file)
        {
            try
            {
                File.Delete(file);

                return true;
            }
            catch
            { }

            return false;
        }

        /// <summary>
        /// Check if file is exist
        /// </summary>
        /// <param name="file">file path,include dir and file name</param>
        /// <returns>true Exist;false Not Exist</returns>
        public static bool FileIsExist(string file)
        {
            return File.Exists(file);
        }

        /// <summary>
        /// get all appoint files from a dir
        /// </summary>
        /// <param name="dir">dir which need to search</param>
        /// <param name="fileExtendedName">file extennsion.for example '.txt'</param>
        /// <param name="hasSubDir">has sub directory?</param>
        /// <returns>group of filenames</returns>
        public static Hashtable GetAllAppointFileFromDir(string dir, string fileExtension, bool hasSubDir)
        {
            Hashtable ht = new Hashtable();

            DirectoryInfo di = new DirectoryInfo(dir);

            if (!di.Exists)
            {
                return ht;
            }

            FileInfo[] fileInfo = di.GetFiles("*" + fileExtension);  //目录下的文件
            foreach (FileInfo fInfo in fileInfo)
            {
                ht.Add(ht.Count, fInfo.FullName);
            }

            if (hasSubDir)
            {
                DirectoryInfo[] subDirectories = di.GetDirectories();//获得子目录

                foreach (DirectoryInfo dirinfo1 in subDirectories)
                {
                    FileInfo[] fileInfo1 = dirinfo1.GetFiles("*" + fileExtension);  //子目录下的文件
                    foreach (FileInfo fInfo1 in fileInfo1)
                    {
                        ht.Add(ht.Count, fInfo1.FullName);
                    }
                }
            }

            return ht;
        }

        /// <summary>
        /// Read a file 's content
        /// </summary>
        /// <param name="file">file path,include dir and file name</param>
        /// <returns>the content of the file</returns>
        public static string ReadFile(string file)
        {
            if (File.Exists(file))
            {
                try
                {
                    StreamReader sr = new System.IO.StreamReader(file);
                    string rel = sr.ReadToEnd();
                    sr.Close();
                    sr.Dispose();

                    return rel;
                }
                catch
                { }
            }

            return null;
        }

        /// <summary>
        /// Save a file
        /// </summary>
        /// <param name="file">file path,include dir and file name</param>
        /// <param name="content">file content which to save</param>
        /// <returns>true save sucess;false save error</returns>
        public static bool SaveFile(string file, string content)
        {
            if (!File.Exists(file))
            {
                string dir = System.IO.Path.GetDirectoryName(file);

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

                try
                {
                    FileStream fs = File.Create(file);
                    fs.Close();
                    fs.Dispose();
                }
                catch
                { return false; }
            }

            try
            {
                StreamWriter sw = new StreamWriter(file, false);
                sw.Write(content);
                sw.Close();
                sw.Dispose();

                return true;
            }
            catch
            { }

            return false;
        }
    }
}

http://www.kler.cn/news/357936.html

相关文章:

  • Docker容器:Docker-harbor 私有仓库的部署与管理
  • 科研绘图系列:R语言柱状图(histogram)
  • linux基础-学习笔记
  • 深度学习中一些好的博客
  • grafana failed to load dashboard from file= ... json error=EOF
  • 获取每个页面的元素,并写入json
  • Spring Boot框架:图书进销存管理的高效工具
  • 使用 cmake 在 x86 系统中为 arm 系统交叉编译程序
  • JS通过递归函数来剔除树结构特定节点
  • C/C++每日一练:合并两个有序数组
  • 【iOS】AFNetworing初步学习
  • openresty“热部署“lua
  • 【Linux】如何通过系统宏定义,获取进程的退出码或退出信号
  • 使用Python在Jupyter Notebook中显示Markdown文本
  • 前端工程师面试题整理
  • 【C语言教程】【嵌入式编程】(一)介绍与前提条件(二)嵌入式编程基础(三)硬件基础知识(四)硬件寄存器操作
  • linux运行openfoam并行会报错:attempt to run parallel on 1 processor
  • 多个版本的GCC(GNU编译器集合)可以同时安装并存
  • 官龙村捐赠图书整理有感
  • Next.js 学习 - 生命周期