Windows 和 Linux 目录名称中禁止使用哪些字符? [英] What characters are forbidden in Windows and Linux directory names?

查看:61
本文介绍了Windows 和 Linux 目录名称中禁止使用哪些字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道/在Linux中是非法的,以下在Windows中是非法的(我认为)* . " / [ ] : ; | ,

I know that / is illegal in Linux, and the following are illegal in Windows (I think) * . " / [ ] : ; | ,

我还缺少什么?

但是,我需要一份综合指南,并且考虑到双字节字符.链接到外部资源对我来说没问题.

I need a comprehensive guide, however, and one that takes into account double-byte characters. Linking to outside resources is fine with me.

我需要首先使用一个名称在文件系统上创建一个目录包含禁止字符,所以我打算用下划线.然后我需要将此目录及其内容写入一个 zip 文件(使用 Java),所以关于 zip 目录名称的任何其他建议将不胜感激.

I need to first create a directory on the filesystem using a name that may contain forbidden characters, so I plan to replace those characters with underscores. I then need to write this directory and its contents to a zip file (using Java), so any additional advice concerning the names of zip directories would be appreciated.

推荐答案

禁止文件名字符的综合指南"不适用于 Windows,因为它保留了文件名和字符.是的,像这样的字符* " ? 等都是被禁止的,但是有无数的名字只由被禁止的有效字符组成.例如空格和点是有效的文件名字符,但禁止仅由这些字符组成的名称.

A "comprehensive guide" of forbidden filename characters is not going to work on Windows because it reserves filenames as well as characters. Yes, characters like * " ? and others are forbidden, but there are a infinite number of names composed only of valid characters that are forbidden. For example, spaces and dots are valid filename characters, but names composed only of those characters are forbidden.

Windows 不区分大小写字符,因此如果名为 a 的文件夹已存在,则您无法创建名为 A 的文件夹.更糟糕的是,看似允许的名称,如 PRNCON 以及许多其他名称,是保留的,不允许使用.Windows 也有几个长度限制;如果移动到另一个文件夹,在一个文件夹中有效的文件名可能会变得无效.的规则命名文件和文件夹位于 Microsoft 文档中.

Windows does not distinguish between upper-case and lower-case characters, so you cannot create a folder named A if one named a already exists. Worse, seemingly-allowed names like PRN and CON, and many others, are reserved and not allowed. Windows also has several length restrictions; a filename valid in one folder may become invalid if moved to another folder. The rules for naming files and folders are on the Microsoft docs.

通常,您不能使用用户生成的文本来创建 Windows 目录名称.如果你想允许用户命名他们想要的任何东西,你必须创建安全名称,如 AABA2 等,存储用户生成的名称及其在应用程序数据文件中的等效路径,并在您的应用程序中执行路径映射.

You cannot, in general, use user-generated text to create Windows directory names. If you want to allow users to name anything they want, you have to create safe names like A, AB, A2 et al., store user-generated names and their path equivalents in an application data file, and perform path mapping in your application.

如果您绝对必须允许用户生成的文件夹名称,则判断它们是否无效的唯一方法是捕获异常并假设名称无效.即便如此也充满危险,因为拒绝访问、脱机驱动器和驱动器空间不足引发的异常与无效名称引发的异常重叠.你正在打开一个巨大的伤害罐头.

If you absolutely must allow user-generated folder names, the only way to tell if they are invalid is to catch exceptions and assume the name is invalid. Even that is fraught with peril, as the exceptions thrown for denied access, offline drives, and out of drive space overlap with those that can be thrown for invalid names. You are opening up one huge can of hurt.

这篇关于Windows 和 Linux 目录名称中禁止使用哪些字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆