获取指定路径所指定文件或目录名称

/// <summary>
        /// 获取指定路径所指定文件或目录名称
        /// 如:C:\windows\test/ 为test ; C:\windows\test\A.doc\ 为A.doc
        /// </summary>
        /// <param name="Path">路径</param>
        /// <returns></returns>
        public static string GetName(string Path)
        {
            try
            {
                Path = RegPath(Path);
                Path=Path.Substring(Path.LastIndexOf("\\") + 1);
            }
            catch
            {
            }
            return Path;
        }

/// <summary>
        /// 将指定的路径变为规则路径(最后不含\)
        /// 如:C:\windows\test
        /// </summary>
        /// <param name="Path"></param>
        /// <returns></returns>
        public static string RegPath(string Path)
        {
            try
            {
                while (true)
                {
                    Path = Path.Trim();
                    Path = Path.Replace("/", "\\");
                    Path = Path.Replace("\\\\", "\\");
                    if (Path.EndsWith("\\") || Path.EndsWith("/"))
                    {
                        Path = Path.Substring(0, Path.Length - 1);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            catch
            {
                Path = null;
            }
            return Path;
        }

 

posted @ 2009-05-27 15:40  起源  阅读(454)  评论(1编辑  收藏  举报