zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    }
  • 相关阅读:
    APUE习题3.2用dup实现dup2以及shell中重定向符号的使用
    如何理解git checkout -- file和git reset HEAD -- file
    bash中通过设置PS1变量改变提示符颜色
    Ubuntu中root的默认密码
    Kali中装中文输入法小企鹅
    Find the Top 10 commands in your linux box!
    简明awk教程(Simple awk tutorial)
    PHP错误解决:Fatal error: Unknown: Failed opening required ...
    简单的端口扫描器(TCP connect)
    c# 爬虫(三) 文件上传
  • 原文地址:https://www.cnblogs.com/girliswater/p/14328602.html
Copyright © 2011-2022 走看看