首页 > 编程语言 >文件夹判断操作类 - C#小函数类推荐

文件夹判断操作类 - C#小函数类推荐

时间:2024-07-30 09:28:23浏览次数:7  
标签:函数 filePath C# 路径 文件夹 根目录 Path string

       此文记录的是关于文件夹操作的小函数。

/***

    文件夹判断操作类

    Austin Liu 刘恒辉
    Project Manager and Software Designer

    E-Mail: [email protected]
    Blog:   http://lzhdim.cnblogs.com
    Date:   2024-01-15 15:18:00

***/

namespace Lzhdim.LPF.Utility
{
    using System;

    using System.IO;

    /// <summary>
    /// 文件夹判断操作类
    /// </summary>
    public class DirDetermineUtil
    {
        /// <summary>
        /// 判断目录和文件是否在同一个目录下
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="directoryPath">文件夹路径</param>
        /// <returns>true 在同一个文件夹;false 不在同一个文件夹</returns>
        public static bool AreInSameDirectory(string filePath, string directoryPath)
        {
            // 获取文件的目录路径
            string fileDirectory = Path.GetDirectoryName(filePath);

            // 标准化路径,移除尾部的斜杠
            fileDirectory = fileDirectory.TrimEnd(Path.DirectorySeparatorChar);
            directoryPath = directoryPath.TrimEnd(Path.DirectorySeparatorChar);

            // 比较两者是否相等
            return string.Equals(fileDirectory, directoryPath, StringComparison.OrdinalIgnoreCase);
        }

        /// <summary>
        /// 获取文件路径所在的目录名
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>文件所在目录</returns>
        public static string GetFileLocatedDirName(string filePath)
        {
            string fileDir = Path.GetDirectoryName(filePath);

            if (fileDir == null)
            {
                //根目录
                return Path.GetPathRoot(filePath);
            }

            int lastIndex = fileDir.LastIndexOf("\\");

            return fileDir.Substring(lastIndex + 1, fileDir.Length - lastIndex - 1);
        }

        /// <summary>
        /// 判断当前文件是否在根目录
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>true 在根目录;false 不在根目录</returns>
        public static bool IsInLocatedRootDir(string filePath)
        {
            //如果没获取到路径的目录,则为根目录
            return Path.GetDirectoryName(filePath) == null;
        }
    }
}

 

标签:函数,filePath,C#,路径,文件夹,根目录,Path,string
From: https://www.cnblogs.com/lzhdim/p/18325737

相关文章

  • opencv 为图像添加边界
    我们经常会有对图像边缘做扩展的需求.比如希望卷积后得到的矩阵大小不变希望改变图像大小,但是不改变宽高比opencv实现opencv中使用copyMakeBorder()来完成这一功能apisrc是原图像矩阵dst是新图像矩阵top/bottom/left/right是边界扩展的大小(比如5就代表5个像素)b......
  • opencv 膨胀与腐蚀
    腐蚀和膨胀Erosion/Dilationerosion/dilation,用白话说,就是让图像亮的区域收缩和扩张.原理我们定义一个卷积核矩阵.这个矩阵可以是任何形状的,但通常而言,是矩形或者圆形的.同时要定义一个锚点位置.用这个卷积核矩阵挨个地划过原始图像矩阵,同时更改锚点位置的像素值.......
  • 传统的cdn技术如何与pcdn技术结合使用
    传统的cdn技术如何与pcdn技术结合使用传统的CDN技术与PCDN技术可以结合使用,以形成互补协作的关系,提供更加高效和优质的内容分发服务。这种结合使用可以通过以下方式实现:1.协同工作:传统的CDN技术主要依赖于中心化的服务器进行内容分发,而PCDN技术则利用用户设备之间的P2P连......
  • opencascade AIS_RubberBand AIS_RotationMode源码学习
    //!相机旋转类型Camerarotationmode.enumAIS_RotationMode{AIS_RotationMode_BndBoxActive,//!<defaultOCCTrotationAIS_RotationMode_PickLast,//!<rotatearoundlastpickedpointAIS_RotationMode_PickCenter,//!<rotatearoundpointatthecenter......
  • Dynamics 365 插件的优缺点
    在现代企业环境中,客户关系管理(CRM)系统如Dynamics365扮演着至关重要的角色。为了满足特定业务需求,企业常常需要对系统进行自定义和扩展。Dynamics365插件(Plugin)正是实现这一目的的重要工具。本文将探讨使用Dynamics365插件的优缺点。插件的优点1.自动化流程插件......
  • PCDN带宽市场现状与前景
    近年来,随着互联网技术的飞速发展,内容分发网络(CDN)行业竞争日益激烈。在这样的市场环境下,PCDN(P2PCDN,即点对点内容分发网络)作为一种新型的CDN技术,逐渐受到了业界的关注。PCDN是以P2P技术为基础,结合CDN技术的一种内容分发方式。它通过挖掘边缘网络的海量碎片化资源,将用户闲......
  • 如何获得 Shiny Chat 的响应来显示格式化的数学方程?
    我试图让这个示例应用程序输出格式化的数学方程。闪亮的聊天教程此处建议自定义响应显示,但我无法获得建议@chat.transform_assistant_response修改格式。我按原样使用下面的代码:@chat.transform_assistant_responsedef_(content:str)->ui.HTM......
  • 聊聊在springcloud gateway如何获取请求体
    前言在我们扩展scg时,获取requestbody也是一个挺常见的需求了,比如记录日志,我们要获取请求体里面的内容。在HTTP协议中,服务器接收到客户端的请求时,请求体(RequestBody)通常是以流的形式传输的。这个流在设计上是只读且不可重复读取的。即requestbody只能读取一次,但我们很多时候是更......
  • Navigating Kubernetes Certification: CKAD vs. CKA vs. CKS, Including KCNA and KC
    https://www.linkedin.com/pulse/navigating-kubernetes-certification-ckad-vs-cka-cks-including-am3ze/ NavigatingKubernetesCertification:CKADvs.CKAvs.CKS,IncludingKCNAandKCSACloudTechLab:KubernetesTraining845followers Dec......
  • centos配置邮箱服务
    1、安装邮箱服务yum-yinstallsendmailmailx2、修改配置setsmtp=smtps://[email protected]#发送方[email protected]#发送方用户名setsmtp-auth-password=*********#密码(非邮箱登录密码)或者密钥setnss-config-dir=/root/.certs......