File.Copy() 或 Directory.CreateDirectory() 方法的路径变量中的额外斜杠

发布于 2024-10-01 11:29:17 字数 401 浏览 4 评论 0原文

当我使用某些 System.IO 方法,并且“不小心”在路径变量中添加了额外的斜杠 (\) 时,似乎没有发生任何特殊情况。没有错误或警告,并且代码的工作方式就像存在适量的斜杠一样。

示例:

Directory.CreateDirectory(@"C:\Users\Public\Documents\\test");
File.Copy(@"C:\Users\Public\\Documents\test.txt", @"C:\Users\\Public\Documents\test\test.txt", true);

所以我想知道,如果上面的代码有时有额外的斜杠,是否有潜在的危险,或者在任何情况下都没有一点关系?

When I use some System.IO methods, and "accidentally" put extra slashes (\) in the path variables, nothing special seems to happen. No errors or warnings, and the code works as if just the right amount of slashes were present.

Example:

Directory.CreateDirectory(@"C:\Users\Public\Documents\\test");
File.Copy(@"C:\Users\Public\\Documents\test.txt", @"C:\Users\\Public\Documents\test\test.txt", true);

So I'm wondering, is it potentially dangerous if the code above had extra slashes sometimes, or would it not matter one iota under any circumstances?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

々眼睛长脚气 2024-10-08 11:29:17

Windows 对此相当有弹性,尚未注意到问题。

查看此线程中的代码片段。请注意 _ACP_INCLUDE 环境变量的值。您必须向右滚动才能看到:

C:\Program Files\Microsoft SDKs\Windows\v6.0A\\include;

Afaik,很多装有 VS2008 的机器都有这个坏路径,我的当然也有。然而,当您解析路径字符串时,它肯定会导致您自己的代码出错。

Windows is quite resilient to this, haven't noticed a problem yet.

Take a look at the snippet in this thread. Note the value of the _ACP_INCLUDE environment variable. You have to scroll to the right to see:

C:\Program Files\Microsoft SDKs\Windows\v6.0A\\include;

Afaik, a lot of machines that have VS2008 have this bad path, mine certainly does. Nevertheless, it certainly can trip up your own code when you parse path strings.

凉墨 2024-10-08 11:29:17

Windows似乎并不介意,但为什么不做出优雅的代码呢?

Windows does not seem to mind, but why not make elegant code?

放我走吧 2024-10-08 11:29:17

我非常确定 Windows 在使用路径结构之前会对其进行“标准化”。但是,为了安全起见,最好使用以下方式组合路径:

Path.Combine(string1, string2);

而不是连接两个字符串。

I'm pretty sure Windows "normalizes" the path structure before using it. To be safe, however, it is best to combine paths using:

Path.Combine(string1, string2);

instead of concatenating two strings.

走过海棠暮 2024-10-08 11:29:17

您应该了解的情况,

CreateFileW(L"D:\\1\\test.txt", GENERIC_READ, 0, 0, CREATE_NEW, 0, 0);
// A file has been created.

CreateFileW(L"D:\\1\\test.txt", GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
// Of course, the file will be opened well.

CreateFileW(L"D:\\1\\\\test.txt", GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
// It works well too. Windows subsystem does canonicalize that.

CreateFileW(L"\\\\?\\D:\\1\\\\test.txt", GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
// '\\?\' prefix tell Windows "Don't touch my path, just send it to filesystem"
// So it will be failed.

http://msdn。 microsoft.com/en-us/library/aa365247(VS.85).aspx#maxpath
我不知道 .NET 如何包装这个 api。我想汉斯·帕桑特可能对此很了解。 :)

The case you should know,

CreateFileW(L"D:\\1\\test.txt", GENERIC_READ, 0, 0, CREATE_NEW, 0, 0);
// A file has been created.

CreateFileW(L"D:\\1\\test.txt", GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
// Of course, the file will be opened well.

CreateFileW(L"D:\\1\\\\test.txt", GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
// It works well too. Windows subsystem does canonicalize that.

CreateFileW(L"\\\\?\\D:\\1\\\\test.txt", GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
// '\\?\' prefix tell Windows "Don't touch my path, just send it to filesystem"
// So it will be failed.

http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx#maxpath
I have no idea how .NET wraps this api. I guess Hans Passant might knows well about this. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文