管理

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

Posted on 2024-10-12 09:30  lzhdim  阅读(9510)  评论(0编辑  收藏  举报

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

/***

    文件工具类

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

 

Copyright © 2000-2022 Lzhdim Technology Software All Rights Reserved