c++ - std::filesystem::canonical converts the mapped network drive to network path - Stack Overflow

For the below code:std::error_code ec;auto long_path = std::filesystem::canonical("W:\31\arawa

For the below code:

std::error_code ec;
auto long_path = std::filesystem::canonical("W:\\31\\arawat.fielsystem_default_check\\matlab\\check_std\\check_std\\").string();

I am getting the value of long_path as:

"\\\\bgl-zp31-cifs\\vmgr\\sbs31\\arawat.fielsystem_default_check\\matlab\\check_std\\check_std"

std::filesystem::canonical() is converting the path to a network UNC path. Is there any way to prevent it, or to get the original drive letter path back from a network path?

I have tried using Windows APIs like WNetGetUniversalName(), etc.

For the below code:

std::error_code ec;
auto long_path = std::filesystem::canonical("W:\\31\\arawat.fielsystem_default_check\\matlab\\check_std\\check_std\\").string();

I am getting the value of long_path as:

"\\\\bgl-zp31-cifs\\vmgr\\sbs31\\arawat.fielsystem_default_check\\matlab\\check_std\\check_std"

std::filesystem::canonical() is converting the path to a network UNC path. Is there any way to prevent it, or to get the original drive letter path back from a network path?

I have tried using Windows APIs like WNetGetUniversalName(), etc.

Share Improve this question edited Mar 10 at 21:51 Remy Lebeau 601k36 gold badges507 silver badges851 bronze badges asked Mar 10 at 12:53 Atul RawatAtul Rawat 191 silver badge1 bronze badge 4
  • 1 Perhaps because the W drive is a network drive? And why is this a problem? – Some programmer dude Commented Mar 10 at 12:55
  • 8 That is literally what "canonical path" means. If you were to mount (some subpath of) that network share to a different drive, you would get back the canonical URL again. This is a feature, not a bug. – Botje Commented Mar 10 at 13:59
  • 4 I don't think that there's an inverse function. You could have W: and X: mapped to the same network location (or a subdirectory thereof). Then the inverse function would need to return 2 results. – Thomas Weller Commented Mar 10 at 14:18
  • The function is performing properly and doing what it's meant to do. Maybe it would help if you explained what result you are expecting in the above example? – Jonathan Potter Commented Mar 12 at 17:32
Add a comment  | 

1 Answer 1

Reset to default 2

is there any way to prevent it ...

Yes, just use std::filesystem::path:

auto long_path = std::filesystem::path("...");

If the path contains relative elements and you'd like to resolve them into a full path, use std::filesystem::absolute:

auto long_path = std::filesystem::absolute("...");

Example:

#include <iostream>
#include <filesystem>

int main()
{
    auto long_path = std::filesystem::path(R"aw(N:\proj\stackoverflow\..)aw");
    std::cout << long_path << '\n';
    std::cout << std::filesystem::absolute(long_path) << '\n';
    std::cout << std::filesystem::canonical(long_path) << '\n';
}

Possible output:

"N:\\proj\\stackoverflow\\.."
"N:\\proj"
"\\\\192.168.1.4\\ted\\proj"

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744845833a4596829.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信