• c# 对文件操作


    /// <summary>
            /// 获得当前绝对路径
            /// </summary>
            /// <param name="strPath">指定的路径</param>
            /// <returns>绝对路径</returns>
            public static string GetMapPath(string strPath)
            {
                if (strPath.ToLower().StartsWith("http://"))
                {
                    return strPath;
                }
                if (HttpContext.Current != null)
                {
                    return HttpContext.Current.Server.MapPath(strPath);
                }
                else //非web程序引用
                {
                    strPath = strPath.Replace("/", "\");
                    if (strPath.StartsWith("\"))
                    {
                        strPath = strPath.Substring(strPath.IndexOf('\', 1)).TrimStart('\');
                    }
                    return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
                }
            }
            /// <summary>
            /// 删除单个文件
            /// </summary>
            /// <param name="_filepath">文件相对路径</param>
            public static bool DeleteFile(string _filepath)
            {
                if (string.IsNullOrEmpty(_filepath))
                {
                    return false;
                }
                string fullpath = GetMapPath(_filepath);
                if (File.Exists(fullpath))
                {
                    File.Delete(fullpath);
                    return true;
                }
                return false;
            }
    
            /// <summary>
            /// 删除上传的文件(及缩略图)
            /// </summary>
            /// <param name="_filepath"></param>
            public static void DeleteUpFile(string _filepath)
            {
                if (string.IsNullOrEmpty(_filepath))
                {
                    return;
                }
                string fullpath = GetMapPath(_filepath); //原图
                if (File.Exists(fullpath))
                {
                    File.Delete(fullpath);
                }
                if (_filepath.LastIndexOf("/") >= 0)
                {
                    string thumbnailpath = _filepath.Substring(0, _filepath.LastIndexOf("/")) + "mall_" + _filepath.Substring(_filepath.LastIndexOf("/") + 1);
                    string fullTPATH = GetMapPath(thumbnailpath); //宿略图
                    if (File.Exists(fullTPATH))
                    {
                        File.Delete(fullTPATH);
                    }
                }
            }
    
            /// <summary>
            /// 删除指定文件夹
            /// </summary>
            /// <param name="_dirpath">文件相对路径</param>
            public static bool DeleteDirectory(string _dirpath)
            {
                if (string.IsNullOrEmpty(_dirpath))
                {
                    return false;
                }
                string fullpath = GetMapPath(_dirpath);
                if (Directory.Exists(fullpath))
                {
                    Directory.Delete(fullpath, true);
                    return true;
                }
                return false;
            }
    
            /// <summary>
            /// 修改指定文件夹名称
            /// </summary>
            /// <param name="old_dirpath">旧相对路径</param>
            /// <param name="new_dirpath">新相对路径</param>
            /// <returns>bool</returns>
            public static bool MoveDirectory(string old_dirpath, string new_dirpath)
            {
                if (string.IsNullOrEmpty(old_dirpath))
                {
                    return false;
                }
                string fulloldpath = GetMapPath(old_dirpath);
                string fullnewpath = GetMapPath(new_dirpath);
                if (Directory.Exists(fulloldpath))
                {
                    Directory.Move(fulloldpath, fullnewpath);
                    return true;
                }
                return false;
            }
    
            /// <summary>
            /// 返回文件大小KB
            /// </summary>
            /// <param name="_filepath">文件相对路径</param>
            /// <returns>int</returns>
            public static int GetFileSize(string _filepath)
            {
                if (string.IsNullOrEmpty(_filepath))
                {
                    return 0;
                }
                string fullpath = GetMapPath(_filepath);
                if (File.Exists(fullpath))
                {
                    FileInfo fileInfo = new FileInfo(fullpath);
                    return ((int)fileInfo.Length) / 1024;
                }
                return 0;
            }
    
            /// <summary>
            /// 返回文件扩展名,不含“.”
            /// </summary>
            /// <param name="_filepath">文件全名称</param>
            /// <returns>string</returns>
            public static string GetFileExt(string _filepath)
            {
                if (string.IsNullOrEmpty(_filepath))
                {
                    return "";
                }
                if (_filepath.LastIndexOf(".") > 0)
                {
                    return _filepath.Substring(_filepath.LastIndexOf(".") + 1); //文件扩展名,不含“.”
                }
                return "";
            }
    
            /// <summary>
            /// 返回文件名,不含路径
            /// </summary>
            /// <param name="_filepath">文件相对路径</param>
            /// <returns>string</returns>
            public static string GetFileName(string _filepath)
            {
                return _filepath.Substring(_filepath.LastIndexOf(@"/") + 1);
            }
    
            /// <summary>
            /// 文件是否存在
            /// </summary>
            /// <param name="_filepath">文件相对路径</param>
            /// <returns>bool</returns>
            public static bool FileExists(string _filepath)
            {
                string fullpath = GetMapPath(_filepath);
                if (File.Exists(fullpath))
                {
                    return true;
                }
                return false;
            }
  • 相关阅读:
    阿里云通过465端口发送邮件绕过25端口
    阿里云子账号Policy授权规则明细
    蓝鲸cmdb平台架构
    库文件缺失问题修复
    centos7.2中启动polkit服务启动失败
    Linux升级GCC
    Ubuntu18.04服务器使用netplan网络构建桥接kvm虚拟机
    常用服务部署脚本(nodejs,pyenv,go,redis,)
    anaconda安装教程(之前安装过python)
    【PHP】array_unique与array_array_flip
  • 原文地址:https://www.cnblogs.com/feizianquan/p/9720854.html
Copyright © 2020-2023  润新知