简体   繁体   English

获取 Windows 中的保留文件名

[英]Get reserved file names in Windows

It's known, you can't create a file with name con ( con.txt for example) in Windows.众所周知,您不能在 Windows 中创建名称为con (例如con.txt )的文件。 So, in my application user can input a custom file name (not via SaveFileDialog, just textbox to input name, not full path), and I want to show an error message, when user inputs invalid file name.因此,在我的应用程序中,用户可以输入自定义文件名(不是通过 SaveFileDialog,只是输入名称的文本框,而不是完整路径),并且我想在用户输入无效文件名时显示错误消息。 Is there a way to get full list of reserved Windows file names ( con , prn , lpt and so on)?有没有办法获取保留的 Windows 文件名( conprnlpt等)的完整列表?

Do not use the following reserved names for the name of a file: 不要将以下保留名称用于文件名:

CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9. CON,PRN,AUX,NUL,COM1,COM2,COM3,COM4,COM5,COM6,COM7,COM8,COM9,LPT1,LPT2,LPT3,LPT4,LPT5,LPT6,LPT7,LPT8和LPT9。

Also avoid these names followed immediately by an extension; 同时避免使用这些名称后立即进行扩展; for example, NUL.txt is not recommended. 例如,不建议使用NUL.txt。

As per: https://msdn.microsoft.com/en-gb/library/windows/desktop/aa365247(v=vs.85).aspx 按照: https//msdn.microsoft.com/en-gb/library/windows/desktop/aa365247(v = vs。85).aspx

Take a look here: Reserved file names 看看这里: 保留的文件名

Reserved filenames in Windows operating systems Windows操作系统中的保留文件名

aux con clock$ nul prn com1 com2 com3 com4 com5 com6 com7 com8 com9 lpt1 lpt2 lpt3 lpt4 lpt5 lpt6 lpt7 lpt8 lpt9 aux con clock $ nul prn com1 com2 com3 com4 com5 com6 com7 com8 com9 lpt1 lpt2 lpt3 lpt4 lpt5 lpt6 lpt7 lpt8 lpt9

Just try to create that file in a %temp% folder. 只是尝试在%temp%文件夹中创建该文件。 If you succeed, than, the name is valid. 如果您成功,则名称有效。 If you fail, you can show the user your message. 如果失败,您可以向用户显示您的消息。

Also, I would check for already existing file with that name before creating. 此外,我会在创建之前检查具有该名称的现有文件。

After all, don't forget to delete it. 毕竟,不要忘记删除它。

CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9 CON,PRN,AUX,NUL,COM1,COM2,COM3,COM4,COM5,COM6,COM7,COM8,COM9,LPT1,LPT2,LPT3,LPT4,LPT5,LPT6,LPT7,LPT8和LPT9

Though you should use your code under try catch block and catch the exception system.IO.DirectoryNotFoundException 虽然您应该在try catch块下使用您的代码并捕获异常system.IO.DirectoryNotFoundException

You can use GetFileType and disallow files of FILE_TYPE_CHAR in your application. 您可以在应用程序中使用GetFileType并禁止FILE_TYPE_CHAR文件。 That also works for reserved names like con.txt etc. 这也适用于保留名称,如con.txt等。

Pass the file name to GetFullPathName .将文件名传递给GetFullPathName If the result starts with \\\\.\\ , it is a device name.如果结果以\\\\.\\开头,则它是设备名称。 Java uses this method . Java 使用这种方法

This method works only with NT-based Windows.此方法仅适用于基于 NT 的 Windows。 If you still support Win9x, you will have to invoke a 16-bit DOS program to traverse the DOS device chain.如果您仍然支持 Win9x,则必须调用 16 位 DOS 程序来遍历 DOS 设备链。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM