在Windows下,如何检查给定的字符串是否为合法/有效的文件名? [英] How do I check if a given string is a legal/valid file name under Windows?

查看:117
本文介绍了在Windows下,如何检查给定的字符串是否为合法/有效的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中包括一个批处理文件重命名功能。用户可以输入目标文件名格式,并且(在替换了该模式中的一些通配符之后)我需要检查它是否将在Windows下成为合法文件名。我尝试使用正则表达式,例如 [a-zA-Z0-9 _] + ,但其中不包含来自多种语言的许多国家/地区特定字符(例如变音符和依此类推)。进行这种检查的最佳方法是什么?

I want to include a batch file rename functionality in my application. A user can type a destination filename pattern and (after replacing some wildcards in the pattern) I need to check if it's going to be a legal filename under Windows. I've tried to use regular expression like [a-zA-Z0-9_]+ but it doesn't include many national-specific characters from various languages (e.g. umlauts and so on). What is the best way to do such a check?

推荐答案

您可以从 Path.GetInvalidPathChars GetInvalidFileNameChars

You can get a list of invalid characters from Path.GetInvalidPathChars and GetInvalidFileNameChars.

UPD:参见 Steve Cooper的建议关于如何在正则表达式中使用它们。

UPD: See Steve Cooper's suggestion on how to use these in a regular expression.

UPD2:请注意,根据MSDN中的备注部分,不能保证从此方法返回的数组包含文件中无效的完整字符集。和目录名称。 提供的答案sixlettervaliables 进入更多细节。

UPD2: Note that according to the Remarks section in MSDN "The array returned from this method is not guaranteed to contain the complete set of characters that are invalid in file and directory names." The answer provided by sixlettervaliables goes into more details.

这篇关于在Windows下,如何检查给定的字符串是否为合法/有效的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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