简体   繁体   English

在 Windows 上获取目录分隔符? ('\', '/', ETC。)

[英]Get directory separator char on Windows? ('\', '/', etc.)

tl;dr: How do I ask Windows what the current directory separator character on the system is? tl;dr:我如何询问 Windows 系统上当前的目录分隔符是什么?


Different versions of Windows seem to behave differently (eg \ and / both work on the English versions, ¥ is apparently on the Japanese version, ₩ is apparently on the Korean version , etc...不同版本的 Windows 似乎表现不同(例如\/都适用于英文版本, ¥ 显然适用于日文版本,₩ 显然适用于韩文版本,等等......

Is there any way to avoid hard-coding this, and instead ask Windows at run time?有什么办法可以避免硬编码,而是在运行时询问 Windows?

Note:笔记:

Ideally, the solution should not depend on a high-level DLL like ShlWAPI.dll , because lower-level libraries also depend on this.理想情况下,解决方案不应依赖于像ShlWAPI.dll这样的高级 DLL,因为低级库也依赖于此。 So it should really either depend on kernel32.dll or ntdll.dll or the like... although I'm having a trouble finding anything at all, whether at a high level or at a low level.所以它实际上应该依赖于kernel32.dllntdll.dll或类似的东西......虽然我很难找到任何东西,无论是在高级别还是在低级别。

Edit:编辑:

A little experimentation told me that it's the Win32 subsystem (ie kernel32.dll ... or is it perhaps RtlDosPathNameToNtPathName_U in ntdll.dll ? not sure, didn't test...) which converts forward slashes to backslashes, not the kernel.一个小实验告诉我,它是 Win32 子系统(即kernel32.dll ......或者它可能是ntdll.dll中的RtlDosPathNameToNtPathName_U ?不确定,没有测试......)它将正斜杠转换为反斜杠,而不是内核。 (Prefixing \\?\ makes it impossible to use forward slashes later in the path -- and the NT native user-mode API also fails with forward slashes.) (前缀\\?\使得后面无法在路径中使用正斜杠 - 并且 NT 本机用户模式 ​​API 也因正斜杠而失败。)

So apparently it's not quite "built into" Windows, but rather just a compatibility feature -- which means you can't just blindly substitute slashes instead of backslashes, because any program which randomly prefixes \\?\ to paths will automatically break on forward slashes.所以显然它不是完全“内置”Windows,而只是一个兼容性功能 - 这意味着你不能只是盲目地用斜杠代替反斜杠,因为任何随机前缀\\?\到路径的程序都会自动中断前进斜线。

I have mixed feelings on what conclusions to make regarding this, but I just thought I'd mention it.我对对此做出什么结论有复杂的感觉,但我只是想我会提到它。

(I tagged this as "path separator" even though that's technically incorrect because the path separator is used for separating paths , not directories ( ; vs. \ ). Hopefully people get what I meant.) (我将其标记为“路径分隔符”,即使这在技术上是不正确的,因为路径分隔符用于分隔路径,而不是目录( ; vs. \ )。希望人们明白我的意思。)

While the and ¥ characters are shown as directory separator symbols in the respective Korean and Japanese windows versions, they are only how those versions of Windows represent the same Unicode code point U+005c as a glyph.虽然¥字符在各自的韩文和日文 Windows 版本中显示为目录分隔符符号,但它们只是这些版本的 Windows 将相同的 Unicode 代码点U+005c表示为字形的方式。 The underlying code point for backslash is still the same across English Windows and the Japanese and Korean windows versions.反斜杠的底层代码点在英文 Windows 以及日文和韩文 Windows 版本中仍然相同。

Extra confirmation for this can be found on this page: http://msdn.microsoft.com/en-us/library/dd374047(v=vs.85).aspx可以在此页面上找到对此的额外确认:http: //msdn.microsoft.com/en-us/library/dd374047 (v=vs.85).aspx

Security Considerations for Character Sets in File Names文件名中字符集的安全注意事项

Windows code page and OEM character sets used on Japanese-language systems contain the Yen symbol ( ¥ ) instead of a backslash ( \ ).日语系统上使用的 Windows 代码页和 OEM 字符集包含日元符号 ( ¥ ) 而不是反斜杠 ( \ )。 Thus, the Yen character is a prohibited character for NTFS and FAT file systems.因此,日元字符是 NTFS 和 FAT 文件系统的禁止字符。 When mapping Unicode to a Japanese-language code page, conversion functions map both backslash (U+005C) and the normal Unicode Yen symbol (U+00A5) to this same character.将 Unicode 映射到日语代码页时,转换函数将反斜杠 (U+005C) 和正常的 Unicode 日元符号 (U+00A5) 映射到同一个字符。 For security reasons, your applications should not typically allow the character U+00A5 in a Unicode string that might be converted for use as a FAT file name.出于安全原因,您的应用程序通常不应允许 Unicode 字符串中的字符 U+00A5 可能被转换为 FAT 文件名。

Also, I don't know of any Windows API function that gets you the system's path separator, but you can rely on it being \ in all circumstances.另外,我不知道有任何 Windows API 函数可以为您提供系统的路径分隔符,但在任何情况下您都可以依赖它为\

http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx#naming_conventions http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx#naming_conventions

The following fundamental rules enable applications to create and process valid names for files and directories, regardless of the file system:以下基本规则使应用程序能够创建和处理文件和目录的有效名称,而不管文件系统如何:

... ...

Use a backslash ( \ ) to separate the components of a path.使用反斜杠 ( \ ) 分隔路径的组成部分。 The backslash divides the file name from the path to it, and one directory name from another directory name in a path.反斜杠将文件名与其路径分开,并将一个目录名与路径中的另一个目录名分开。 You cannot use a backslash in the name for the actual file or directory because it is a reserved character that separates the names into components.您不能在实际文件或目录的名称中使用反斜杠,因为它是将名称分隔为组件的保留字符。

... ...

About /关于/

Windows should support the use of / as a directory separator in the API functions, though not necessarily in the command prompt ( command.com ). Windows 应该支持在 API 函数中使用/作为目录分隔符,但不一定在命令提示符 ( command.com ) 中使用。

Note File I/O functions in the Windows API convert "/" to "\" as part of converting the name to an NT-style name, except when using the "\?\" prefix as detailed in the following sections.注意 Windows API 中的文件 I/O 函数将“/”转换为“\”作为将名称转换为 NT 样式名称的一部分,除非使用“\?\”前缀,如以下部分所述。

It's 'tough' to figure out the truth of all this, but this might be a really helpful link about / in Windows paths: http://bytes.com/topic/python/answers/23123-when-did-windows-start-accepting-forward-slash-path-separator弄清楚这一切的真相是“困难的”,但这可能是一个关于 Windows 路径中/的非常有用的链接:http: //bytes.com/topic/python/answers/23123-when-did-windows-start -接受正斜杠路径分隔符

The original poster added the phrase "kernel-mode" in a comment to someone else's answer.原始发帖人在对其他人的回答的评论中添加了短语“内核模式”。

If the original question intended to ask about kernel mode, then it probably isn't a good idea to depend on / being a path separator.如果最初的问题是要询问内核模式,那么依赖 / 作为路径分隔符可能不是一个好主意。 Different file systems allow different character sets on disk.不同的文件系统允许在磁盘上使用不同的字符集。 Different file system drivers in Windows can also allow different characters sets, which normally cannot include characters which the underlying file systems don't accept on disk, but sometimes they can behave strangely. Windows 中不同的文件系统驱动程序也可以允许不同的字符集,这些字符集通常不能包含底层文件系统在磁盘上不接受的字符,但有时它们的行为可能会很奇怪。 For example Posix mode allows a component name to contain some characters in a path name in an NTFS partition, even though NTFS ordinarily doesn't allow those characters.例如,Posix 模式允许组件名称在 NTFS 分区的路径名称中包含某些字符,即使 NTFS 通常不允许这些字符。 (But obviously / isn't one of them, in Posix.) (但显然 / 不是其中之一,在 Posix 中。)

In kernel mode in Unicode, U+005C is always a backslash and it is always the path separator.在 Unicode 的内核模式中,U+005C 始终是反斜杠,并且始终是路径分隔符。 Unicode code points for yen and won are not U+005C and are not path separators.日元和韩元的 Unicode 代码点不是 U+005C,也不是路径分隔符。

In kernel mode in ANSI, complications arise depending on which ANSI code page.在 ANSI 的内核模式下,复杂性取决于哪个 ANSI 代码页。 In code pages that are sufficiently similar to ASCII, 0x5C is a backslash and it is the path separator.在与 ASCII 非常相似的代码页中,0x5C 是一个反斜杠,它是路径分隔符。 In ANSI code pages 932 and 949, 0x5C is not a backslash but 0x5C might be a path separator depending on where it occurs.在 ANSI 代码页 932 和 949 中,0x5C 不是反斜杠,但 0x5C 可能是路径分隔符,具体取决于它出现的位置。 If 0x5C is the first byte of a multibyte character, then it's a yen sign or won sign and it is a path separator.如果 0x5C 是多字节字符的第一个字节,则它是日元符号或韩元符号,它是路径分隔符。 If 0x5C is the second byte of a multibyte character, then it's not a character by itself, so it's not a yen sign or won sign and it's not a path separator.如果 0x5C 是多字节字符的第二个字节,那么它本身就不是字符,所以它不是日元符号或韩元符号,也不是路径分隔符。 You have to start parsing from the beginning of the string to figure out if a particular char is actually a whole character or not.您必须从字符串的开头开始解析,以确定特定字符是否实际上是整个字符。 Also in Chinese and UTF-8, multibyte characters can be longer than two chars.同样在中文和 UTF-8 中,多字节字符可以长于两个字符。

The standard forward slash ( / ) has always worked in all versions of DOS and Windows.标准正斜杠 ( / ) 在所有版本的 DOS 和 Windows 中始终有效。 If you use it, you don't have to worry about issues with how the backslash is displayed on Japanese and Korean versions of Windows, and you also don't have to special-case the path separator for Windows as opposed to POSIX (including Mac).如果您使用它,您不必担心反斜杠在日文和韩文版本的 Windows 上的显示方式问题,并且您也不必为 Windows 而不是 POSIX 特殊情况下的路径分隔符(包括苹果电脑)。 Just use forward slash everywhere.只需在任何地方使用正斜杠即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从函数返回字符串(字符数组、字符指针等) - Returning a string (char array, char pointer etc.) from a function 为什么putchar,toupper,tolower等采用int而不是char? - Why putchar, toupper, tolower, etc. take a int instead of a char? 如何获得/ etc /目录 - How to get /etc/ directory 如何复制`strtod`等的功能而不会收到警告“从'const char *'分配给'char *'丢弃限定符”? - How to replicate the functionality of `strtod`, etc. without getting the warning “Assigning to 'char *' from 'const char *' discards qualifier”? Char作为C中的小数点分隔符 - Char as a decimal separator in C 如果 C 只能返回一个值(int、char 等),那么它如何返回字符串文字(字符数组)? - If C can return only one value(int , char etc.) then how does it returns character string literal(array of chars)? feholdexcept 等有什么用? - What is the use of feholdexcept etc.? 如何使用ETW(StartTrace等)实时获取事件? - How to get events in real-time using ETW (StartTrace etc.)? 为什么C标准提供不定尺寸的类型(int,long long,char与int32_t,int64_t,uint8_t等)? - Why does the C standard provide unsized types (int, long long, char vs. int32_t, int64_t, uint8_t etc.)? C 中的 snprintf 与 strcpy(等) - snprintf vs. strcpy (etc.) in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM