如何转换真实路径CString C ++

时间:2018-10-23 04:58:14

标签: c++ mfc

我有一个真实的Windows路径:

CString path = "C:\Programs File (x86)\Program folder\exec.exe";

如何将其转换为

CString path = "C:\\Programs File (x86)\\Program folder\\exec.exe";

我用path.Replace(L"\\", L"\\\\");尝试过,但是失败了。

1 个答案:

答案 0 :(得分:1)

在您的评论之一中,您写道您的路径是从注册表中读取的。

如果路径实际上是C:\Programs File (x86)\Program folder\exec.exe,则无需转换任何内容。该路径已经有效。

仅当您使用字符串文字时,才需要双\\

示例:

假设您的注册表值逐字包含C:\Programs File (x86)\Program folder\exec.exe

程序段1:

CString somepath = YourGetFromRegistryFunction();
// now somepath contains the correct path already

AfxMessageBox(somepath);  // show the path for debugging purposes

程序段2:

CString somepath = "C:\\Programs File (x86)\\Program folder\\exec.exe";
// now somepath contains the correct path already, the compiler
// replaces the `\\` with a single `\\`

AfxMessageBox(somepath);  // show the path for debugging purposes