zoukankan      html  css  js  c++  java
  • C# 文件绝对路径与相对路径的转换

     class Program
        {
            const string CONFIG_PATH = @"C:SoftWareConfig.xml";
            const string IMAGE_PATH = @"C:SoftWareResourceImg1.png";
            const string DATA_DIR = @"C:SoftWareData";
    
            /// <summary>
            /// Creates a relative path from one file
            /// or folder to another.
            /// </summary>
            /// <param name="fromDirectory">
            /// Contains the directory that defines the
            /// start of the relative path.
            /// </param>
            /// <param name="toPath">
            /// Contains the path that defines the
            /// endpoint of the relative path.
            /// </param>
            /// <returns>
            /// The relative path from the start
            /// directory to the end path.
            /// </returns>
            /// <exception cref="ArgumentNullException"></exception>
            static string MakeRelative(string fromDirectory, string toPath)
            {
                if (fromDirectory == null)
                    throw new ArgumentNullException("fromDirectory");
    
                if (toPath == null)
                    throw new ArgumentNullException("toPath");
    
                bool isRooted = (Path.IsPathRooted(fromDirectory) && Path.IsPathRooted(toPath));
    
                if (isRooted)
                {
                    bool isDifferentRoot = (string.Compare(Path.GetPathRoot(fromDirectory), Path.GetPathRoot(toPath), true) != 0);
    
                    if (isDifferentRoot)
                        return toPath;
                }
    
                List<string> relativePath = new List<string>();
                string[] fromDirectories = fromDirectory.Split(Path.DirectorySeparatorChar);
    
                string[] toDirectories = toPath.Split(Path.DirectorySeparatorChar);
    
                int length = Math.Min(fromDirectories.Length, toDirectories.Length);
    
                int lastCommonRoot = -1;
    
                // find common root
                for (int x = 0; x < length; x++)
                {
                    if (string.Compare(fromDirectories[x], toDirectories[x], true) != 0)
                        break;
    
                    lastCommonRoot = x;
                }
    
                if (lastCommonRoot == -1)
                    return toPath;
    
                // add relative folders in from path
                for (int x = lastCommonRoot + 1; x < fromDirectories.Length; x++)
                {
                    if (fromDirectories[x].Length > 0)
                        relativePath.Add("..");
                }
    
                // add to folders to path
                for (int x = lastCommonRoot + 1; x < toDirectories.Length; x++)
                {
                    relativePath.Add(toDirectories[x]);
                }
    
                // create relative path
                string[] relativeParts = new string[relativePath.Count];
                relativePath.CopyTo(relativeParts, 0);
    
                string newPath = string.Join(Path.DirectorySeparatorChar.ToString(), relativeParts);
    
                return newPath;
            }
            static string MakeAbsolute(string fromPath, string toPath)
            {
                string temp = Path.Combine(fromPath, toPath);
                return Path.GetFullPath(temp);
            }
    
            static void Main(string[] args)
            {
                string pImageRelativePath = MakeRelative(CONFIG_PATH, IMAGE_PATH);
                // ..ResourceImg1.png
                Console.WriteLine("图片文件的相对路径为:{0}", pImageRelativePath);
                
                string pImageAbsolutePath = MakeAbsolute(CONFIG_PATH, pImageRelativePath);
                //C:SoftWareResourceImg1.png
                Console.WriteLine("图片文件的绝对路径为:{0}", pImageAbsolutePath);
    
                string pDataRelativeDir = MakeRelative(CONFIG_PATH, DATA_DIR);
                // ..Data
                Console.WriteLine("数据文件夹的相对路径:{0}", pDataRelativeDir);
    
                string pDataAbsoluteDir = MakeAbsolute(CONFIG_PATH, pDataRelativeDir);
                // C:SoftWareData
                Console.WriteLine("数据文件夹的绝对路径:{0}", pDataAbsoluteDir);
            }
    
    
        }
    
  • 相关阅读:
    分布式01-Dubbo基础背景
    项目总结17-使用layui table分页表格
    项目总结16-创建验证码图片
    Springboot学习07-数据源Druid
    Springboot学习06-Spring AOP封装接口自定义校验
    Springboot学习05-自定义错误页面完整分析
    Springboot学习04-默认错误页面加载机制源码分析
    Springboot学习03-SpringMVC自动配置
    项目总结15:JavaScript模拟表单提交(实现window.location.href-POST提交数据效果)
    Springboot学习02-webjars和静态资源映射规则
  • 原文地址:https://www.cnblogs.com/MaFeng0213/p/8919919.html
Copyright © 2011-2022 走看看