zoukankan      html  css  js  c++  java
  • [No000083]文件与文件夹操作

            #region Folder option 文件夹操作
    
            /// <summary>
            /// 指定目录是否存在
            /// </summary>
            /// <param name="dirPath">文件夹路径</param>
            /// <returns></returns>
            public static bool DirExist(string dirPath)
            {
                try
                {
                    if (Directory.Exists(dirPath))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("DirExit error" + ex.Message);
                    return false;
                }
            }
    
            /// <summary>
            /// 创建目录
            /// </summary>
            /// <param name="dirPath"></param>
            public static void MakeDir(string dirPath)
            {
                try
                {
                    if (!DirExist(dirPath))
                    {
                        Directory.CreateDirectory(dirPath);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("MakeDir error" + ex.Message);
                    //throw;
                }
            }
    
            #endregion Folder option 文件夹操作
    
            #region File option 文件操作
    
            /// <summary>
            /// 指定文件是否存在
            /// </summary>
            /// <param name="filePath"></param>
            /// <returns></returns>
            public static bool FileExist(string filePath)
            {
                try
                {
                    if (File.Exists(filePath))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("FileExist error" + ex.Message);
                    return false;
                }
            }
    
            /// <summary>
            /// 读文件
            /// </summary>
            /// <param name="filePath">文件路径</param>
            /// <returns></returns>
            public static string ReadFile(string filePath)
            {
                try
                {
                    string content = "";
                    if (FileExist(filePath))
                    {
                        StreamReader sr = new StreamReader(filePath, Encoding.UTF8);
                        content = sr.ReadToEnd(); //读取全部内容
                        sr.Close();
                    }
                    return content;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("ReadFile error" + ex.Message);
                    return "";
                }
            }
    
            /// <summary>
            /// 写文件
            /// </summary>
            /// <param name="filePath">文件路径</param>
            /// <param name="content">写入的内容</param>
            public static void WriteFile(string filePath, string content)
            {
                try
                {
                    string fileDirPath = Path.GetDirectoryName(filePath);
                    if (!DirExist(fileDirPath))
                    {
                        MakeDir(fileDirPath);
                    }
                    FileStream fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); //创建写入文件
                    StreamWriter sw = new StreamWriter(fileStream, Encoding.UTF8);
                    sw.WriteLine(content); //开始写入值
                    fileStream.Flush(); //确保全部写入
                    sw.Close();
                    fileStream.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("WriteFile error" + ex);
                }
            }
    
            #endregion File option 文件操作
  • 相关阅读:
    漫谈企业级SaaS的多租户设计
    网易实战分享|云信IM SDK接口设计实践
    WebRTC系列之音频的那些事
    如何科学地完成一场 AR 发布会?全在这份超细节活动策划 Xmind 里了
    移动社交如何玩?网易云信携手崽崽和Uki打造更多新场景!
    行业观察|智慧屏集中爆发,大屏市场能否迎来破局者?
    Docker文件系统实战
    文字检测模型EAST应用详解 ckpt pb的tf加载,opencv加载
    opencv dnn加载EAST的pb模型的一点问题
    百度开源:PaddleOCR与PaddlePaddle / paddle2onnx 实践一
  • 原文地址:https://www.cnblogs.com/Chary/p/No000083.html
Copyright © 2011-2022 走看看