• C#FileHelper文件IO


    C#FileHelper

    /**
    *┌──────────────────────────────────────────────────────────────┐
    *│ 描    述:文件IO的工具类                                                   
    *│ 作    者:执笔小白                                              
    *│ 版    本:4.0                                       
    *│ 创建时间:2022-6-13 15:40:56                            
    *└──────────────────────────────────────────────────────────────┘
    *┌──────────────────────────────────────────────────────────────┐
    *│ 命名空间: ZhibiXiaobai.Uril.IOHelper                               
    *│ 类    名:FileIOHelper                                     
    *└──────────────────────────────────────────────────────────────┘
    */
    using System.IO;
    using System.Windows.Forms;
    
    namespace ZhibiXiaobai.Uril.IOHelper
    {
    
        public class FileIOHelper
        {
            /// <summary>
            /// 判断文件存不存在
            /// <param name="FilePath">文件路径</param>
            /// <param name="IsCreate">不存在时是否创建文件</param>
            /// <returns>返回判断的结果</returns>
            public static bool ISExists_File(string FilePath, bool IsCreate = false)
            {
    
                if (File.Exists(FilePath))
                {
                    return true;
                }
                else
                {
                    if (IsCreate)  // 需不需要创建文件
                    {
                        //检验目录不存在就创建
                        //if (!System.IO.Directory.Exists(FilePath))
                        //{
                        //    System.IO.Directory.CreateDirectory(FilePath);
                        //}
    
                        FilePath = FilePath.Replace(@"\\", @"/");  // 格式化目录 '\\'变为'/'
    
                        string[] ss = FilePath.Split(new char[] { '/' });
                        string filePathP = ss[0];
                        for (int i = 1; i < ss.Length - 1; i++)
                        {
                            filePathP += "/" + ss[i];
                        }
    
                        if (!System.IO.Directory.Exists(filePathP))
                        {
                            System.IO.Directory.CreateDirectory(filePathP);
                        }
                        File.Create(FilePath);
                    }
                    return false;
                }
            }
    
            #region 通过路径取文件方法
            /// <summary>
            /// 通过文件对话框取文件路径
            /// 使用 var filename = OpenfileDlg();
            /// </summary>
            /// <param name="Defaultpath">默认打开路径</param>
            /// <param name="Fileter">文件类型</param>
            /// <param name="Multiselect">是否支持选择多个文件</param>
            /// <param name="DlgTitle">Dlg窗体的标题</param>
            /// <param name="LimitSize">是否限制大小(单位为KB,0为不限制)</param>
            /// <returns></returns>
            public static string OpenfileDlg(string Defaultpath = null, string Fileter = ".jpeg|*.jpeg|.jpg|*.jpg|.png|*.png|.gif|*.gif|.bmp|*.bmp", bool Multiselect = false,
                string DlgTitle = "请选择要打开的文件",long LimitSize=0)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Title = DlgTitle;                // 标题
                ofd.Multiselect = Multiselect;       // 多选
                ofd.InitialDirectory = Defaultpath;  // 初始目录
                ofd.Filter = Fileter;                // 设定文件类型
                ofd.ShowDialog().GetHashCode();
    
                string path = ofd.FileName;  // 获得在打开文件对话框中选择的文件的路径
                if (!string.IsNullOrEmpty(path) && LimitSize > 0 && ofd.OpenFile().Length > (LimitSize * 1024))
                {
                    path = null;
                }
                return path;
            }
            #endregion
    
            /// <summary>
            /// 复制-不覆盖已有文件
            /// </summary>
            /// <param name="SourceFileUrl">源文件地址</param>
            /// <param name="DistFileUrl">目标地址</param>
            public static void CopyFile(string SourceFileUrl,string DistFileUrl)
            {
                File.Copy(SourceFileUrl, DistFileUrl);
            }
        }
    }
  • 相关阅读:
    【未完成】【oracle】add_months与add_days
    【java】[null]的问题
    PID控制算法研究
    matlab函数:residue和residuez的用法
    Windows入门基础:2.vs2013中Icon显示
    关于for()循环使用过程中遇到的问题(俄罗斯方块游戏中遇到的问题)
    Windows入门基础:1.关于CreateWindow()函数使用中遇到的问题
    error C4996: 'swprintf': swprintf has been changed to conform with the ISO C standard,set _CRT_NON_CONFORMING_SWPRINT
    QT5.3.2与VS2013 fatal error LNK1112: 模块计算机类型“X86”与目标计算机类型“x64”冲突_(解决方案)
    Java如何访问private变量
  • 原文地址:https://www.cnblogs.com/qq2806933146xiaobai/p/15901734.html
Copyright © 2020-2023  润新知