管理

文件路径分离操作类 - C#小函数类推荐

Posted on 2025-09-15 16:35  lzhdim  阅读(3508)  评论(0)    收藏  举报
/***

    文件路径分离操作类

    Austin Liu 刘恒辉
    Project Manager and Software Designer

    E-Mail: lzhdim@163.com
    Blog:   http://lzhdim.cnblogs.com
    Date:   2024-01-15 15:18:00

    使用方法例子:
        FileSeparateUtil.GetPath(Application.ExecutablePath());
        FileSeparateUtil.GetParams(Application.ExecutablePath() + " /h /run /proxy");

***/

namespace Lzhdim.LPF.Utility
{
    /// <summary>
    /// 文件路径分离操作类
    /// </summary>
    public sealed class FileSeparateUtil
    {
        /// <summary>
        /// 获取路径中的所有参数
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>所有参数</returns>
        public static string GetParams(string filePath)
        {
            int index = filePath.IndexOf('/');

            if (index > 0)
            {
                return filePath.Substring(index, filePath.Length - index).Trim();
            }
            else
            {
                index = filePath.IndexOf('-');
                if (index > 0)
                {
                    return filePath.Substring(index, filePath.Length - index).Trim();
                }
            }

            return "";
        }

        /// <summary>
        /// 获取文件路径
        /// </summary>
        /// <param name="filePath">文件路径(包括参数)</param>
        /// <returns>文件路径</returns>
        public static string GetPath(string filePath)
        {
            int index = filePath.IndexOf('/');

            if (index > 0)
            {
                return filePath.Substring(0, index).Trim();
            }
            else
            {
                index = filePath.IndexOf('-');
                if (index > 0)
                {
                    int iExt = filePath.LastIndexOf('.');
                    if (iExt < index)
                    {
                        //如果扩展名在-参数之前,说明有-参数
                        return filePath.Substring(0, index).Trim();
                    }
                }
            }

            return filePath.Trim();
        }
    }
}

 

Copyright © 2000-2022 Lzhdim Technology Software All Rights Reserved