FileHelper

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Huinaozn.ASleepPC.Tools.Helper
{
    static class FileHelper
    {
        /// <summary>
        /// 获取路径所有文件
        /// </summary>
        public static List<string> GetFiles(string path)
        {
            try
            {
                if (!Directory.Exists(path))  //路径不存在
                    return null;
                List<string> result = new List<string>();
                DirectoryInfo dir = new DirectoryInfo(path);
                DirectoryInfo[] subfolder = dir.GetDirectories();
                if (subfolder != null && subfolder.Length > 0)
                {
                    foreach (DirectoryInfo item in subfolder)
                    {
                        List<string> subFiles = GetFiles(item.FullName);  //FullName:完整路径
                        if (subFiles != null && subFiles.Count > 0)
                            result.AddRange(subFiles);
                    }
                }
                FileInfo[] files = dir.GetFiles();
                if (files != null)
                {
                    foreach (FileInfo file in files)
                    {
                        result.Add(file.FullName);
                    }
                }
                return result;
            }
            catch (IOException ioe)
            {
                throw new Exception(ioe.Message);
            }

        }

        /// <summary>
        /// 获取文本文件内容
        /// </summary>
        public static string GetTxtFileContent(string fileName)
        {
            try
            {
                if (!File.Exists(fileName))
                {
                    return null;
                }

                using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    lock (fs)
                    {
                        using (StreamReader reader = new StreamReader(fs, new UTF8Encoding(false)))
                        {
                            return reader.ReadToEnd();
                        }
                    }
                }
            }
            catch (IOException ioe)
            {
                throw new Exception(ioe.Message);
            }
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        public static bool DeleteFile(string fileName)
        {
            try
            {
                if (!File.Exists(fileName))
                {
                    return false;
                }

                File.Delete(fileName);
            }
            catch (IOException ioe)
            {
                throw new ArgumentNullException(ioe.Message);
            }

            return true;
        }

        /// <summary>
        /// 覆盖文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="content"></param>
        public static void OverwriteFile(string fileName, string content)
        {
            try
            {
                Directory.CreateDirectory(Path.GetDirectoryName(fileName));

                using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
                {
                    lock (fs)
                    {
                        if (content != null)
                        {
                            using (StreamWriter sw = new StreamWriter(fs, new UTF8Encoding(false)))
                            {
                                sw.Write(content);
                            }
                        }
                    }
                }
            }
            catch (IOException ioe)
            {                                   
                throw new Exception(ioe.Message);
            }
        }

        /// <summary>
        /// 覆盖文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="content"></param>
        public static void OverwriteFile(string fileName, byte[] content)
        {
            try
            {
                Directory.CreateDirectory(Path.GetDirectoryName(fileName));

                using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
                {
                    lock (fs)
                    {
                        if (content != null)
                        {
                            fs.Write(content, 0, content.Length);
                        }
                    }
                }
            }
            catch (IOException ioe)
            {
                throw new Exception(ioe.Message);
            }
        }

        /// <summary>
        /// 根据文件名返回临时文件夹中唯一命名的文件的完整路径
        /// 形如:公司文档(1).doc,公司文档(2).doc
        /// </summary>
        public static string GetTempPathFileName(string fileName)
        {
            // 系统临时文件夹
            string tempPath = Path.GetTempPath();
            // 文件的完成路径
            fileName = tempPath + Path.GetFileName(fileName);
            // 文件名
            string fileNameWithoutExt =
            Path.GetFileNameWithoutExtension(fileName);
            // 扩展名
            string fileExt = Path.GetExtension(fileName);
            int i = 0;
            while (File.Exists(fileName))
            {
                // 生成类似这样的文件名:公司文档(1).doc,公司文档(2).doc
                fileName = tempPath + fileNameWithoutExt +
                string.Format("({0})", ++i) + fileExt;
            }
            return fileName;
        }
    }
}

 

posted @ 2021-01-26 09:11  春江花月夜、  阅读(105)  评论(0编辑  收藏  举报