C#获取上级文件夹路径

/// <summary>
        /// 获取上级文件夹路径
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="degree">级数</param>
        /// <returns></returns>
        private string GetParentDirectory(string path, int degree)
        {
            DirectoryInfo pathInfo = new DirectoryInfo(path);
            string newPath = pathInfo.Parent.FullName;

            for (int i = 0; i < degree; i++)
            {
                pathInfo = new DirectoryInfo(newPath);
                newPath = pathInfo.Parent.FullName;
            }
            return newPath;
        }

 

posted @ 2020-10-13 14:28  be--yourself  阅读(2177)  评论(0编辑  收藏  举报