解析相对路径而不引用Windows上的当前目录 [英] Resolving a relative path without referencing the current directory on Windows

查看:1184
本文介绍了解析相对路径而不引用Windows上的当前目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows cpp应用程序,我想解决相对于特定目录的路径,而不更改当前目录。这意味着我不能使用 GetFullPathName ,因为它解析相对于当前目录的路径。因为这是一个安全敏感的问题,我宁愿不滚自己,但使用一个受制裁的API。

I have a windows cpp app and I want to resolve paths relative to a specific directory without changing the current directory. This means I can't use GetFullPathName since it resolves paths relative to the current directory. Since this is a security sensitive issue I would rather not roll my own but use a sanctioned API.

我找了一个好的答案,但找不到任何东西。当然,这是Web服务器或多线程环境的常见问题。其他人怎么做?

I looked around for a good answer, but couldn't find anything. Surely this is a common issue for web servers or multithreaded environments. How do other people do this? Any help would be greatly appreciated.

例如:您可以从 C:\appfolder\exes\myapp.exe ,而数据位于 C:\appfolder\data 中。我要解决 sillyfolder\..\mydata.txt C:\appfolder\data\mydata.txt

For instance: You run your app from C:\appfolder\exes\myapp.exe while the data is in C:\appfolder\data. I want to resolve sillyfolder\..\mydata.txt to C:\appfolder\data\mydata.txt.

推荐答案

标准(C ++或Win32)库不会为你这样做。但是制作相对路径是一个很好的练习。以下是一些有用的链接,可以了解如何开始(将脚本视为伪代码):

The standard (C++ or Win32) libraries do not do this for you, it seems. But making a relative path is a good exercise. Here are a few useful links to see how to start (treat scripts as pseudocode):

  • Converting an absolute path to a relative path
  • Windows CMD: Get relative path from absolute path
  • MakeRelative - makes a file name relative to a base path

将绝对路径名转换为相对路径名

Converting an absolute pathname to relative requires


  • 应转换的给定目标路径

  • the given target path which should be converted
  • a path which is the current directory.

转换将以字符串操作的方式完成:

The conversion would be done as a string-manipulation:


  • 通过查找以路径分隔符结尾的最长公共前缀开始。

  • 如果没有公共前缀


  • 中的每个目录名称替换为

  • 在目标字符串前添加(使用路径分隔符)


  • start by finding the longest common prefix which ends with a path-separator.
  • if there is no common prefix, you are done
  • strip the common prefix from (a copy of...) the current and target strings
  • replace each directory-name in the current string with ".."
  • add that (with a path-separator) in front of the target string
  • return that combined string

与Unix路径名不同(除了分隔符的/和\之外),Windows路径名有额外的并发症:

Unlike Unix pathnames (aside from the sense of "/" versus "\" for separators), Windows pathnames have additional complications:


  • 在同一台机器上,他们可以有设备名称(一个字母后跟一个:)

  • UNC路径名,以\\ \\。

  • within the same machine, they can have device names (a letter followed by a ":")
  • UNC pathnames, which begin with "\".

对于后一种情况(UNC路径),如果当前 路径不在同一主机上,则相对路径是无意义的。但是,设备名称和路径名不是紧密绑定在一起的(请注意,Windows添加了更改设备与chdir以后的能力,作为选项),并且根据应用程序,可以通过仅考虑路径部分在设备之间创建相对路径名。

For the latter case (UNC paths), if the current and target paths are not on the same host, then relative paths are meaningless. However, device names and pathnames are not that tightly bound together (note that Windows added the ability to change devices with chdir later, as an option), and depending on the application one could make relative pathnames between devices by considering only the path part.

这篇关于解析相对路径而不引用Windows上的当前目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆