• 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;
            }
        }
    }
  • 相关阅读:
    Blend3中创建的Silverlight程序在设计模式下无法显示图片的解决办法
    创建Silverlight Bussiness Application时报错的解决
    .NET 2.0 字符串比较
    ASP.NET 客户端缓存
    AjaxPro部署成功
    遭遇反序列化异常:"在分析完成之前就遇到流结尾"
    正则表达式
    哈哈,终于申请获得批准了!
    ClientScript.RegisterClientScriptInclude注册脚本
    今天经过一场深有体会的谈话终于决定了我2012的方向
  • 原文地址:https://www.cnblogs.com/girliswater/p/14328602.html
Copyright © 2020-2023  润新知