gpt4 book ai didi

c# - .NET 关于文件路径的奇怪行为

转载 作者:可可西里 更新时间:2023-11-01 13:26:38 27 4
gpt4 key购买 nike

我无法通过 Google 教授找到任何关于此的信息,所以我来了。获取给定的路径名​​并将其粘贴到 Windows 资源管理器中。在我的代码中发现生成带有额外“.”的路径的错误后,我偶然发现了这个问题。在目录 \ 分隔符之前的路径名...

@"C:\\pathto.\file.ext"

在代码中,.NET 会在调用 File.Create 时接受路径并生成一个文件,但在这个路径:

@"C:\\pathto\file.ext"

C:\\pathto.\file.ext 复制到 Windows Explorer 的地址栏中并观察 '.'消失并带你到 C:\\pathto\file.ext

.NET 和 Windows 的正常行为是否会导致问题,因为 '.'当传递到文件操作中时,.NET 和 Windows 都会将其删除。真正的问题是数据库中的所有文件的文件名都带有“.\”,但存在于没有“.\”的路径中......并且 File.Exists() 有效同样,虽然路径不是“真实”的物理位置......

这是怎么回事?

最佳答案

这是 Windows 文件系统的一个“特性”。 MSDN :

Do not end a file or directory name with a space or a period. Although the underlying file system may support such names, the Windows shell and user interface does not. However, it is acceptable to specify a period as the first character of a name. For example, ".temp".

关于c# - .NET 关于文件路径的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15642420/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com