tom*_*ash 156 c# windows filesystems file
我想在我的应用程序中包含批处理文件重命名功能.用户可以键入目标文件名模式和(在替换模式中的一些通配符之后)我需要检查它是否是Windows下的合法文件名.我试过使用正则表达式,[a-zA-Z0-9_]+但它不包括来自各种语言的许多国家特定字符(例如变音符号等).做这种检查的最佳方法是什么?
use*_*116 120
从MSDN的"命名文件或目录",以下是Windows下合法文件名的一般约定:
您可以使用当前代码页中的任何字符(Unicode/ANSI高于127),除了:
< > : " / \ | ? *一些可选的东西要检查:
\?\前缀)\?\(请注意,前缀可能会扩展目录组件并导致其溢出32,000个限制)Eug*_*atz 97
您可以从Path.GetInvalidPathChars和获取无效字符列表GetInvalidFileNameChars.
UPD:请参阅Steve Cooper关于如何在正则表达式中使用这些内容的建议.
UPD2:请注意,根据MSDN中的"备注"部分,"不保证从此方法返回的数组包含在文件和目录名称中无效的完整字符集".sixlettervaliables提供的答案更详细.
Ste*_*per 63
对于3.5之前的.Net框架,这应该工作:
正则表达式匹配应该可以帮到你.这是使用System.IO.Path.InvalidPathChars常量的片段;
bool IsValidFilename(string testName)
{
Regex containsABadCharacter = new Regex("["
+ Regex.Escape(System.IO.Path.InvalidPathChars) + "]");
if (containsABadCharacter.IsMatch(testName)) { return false; };
// other checks for UNC, drive-path format, etc
return true;
}
Run Code Online (Sandbox Code Playgroud)
对于3.0之后的.Net框架,这应该工作:
http://msdn.microsoft.com/en-us/library/system.io.path.getinvalidpathchars(v=vs.90).aspx
正则表达式匹配应该可以帮到你.这是使用System.IO.Path.GetInvalidPathChars()常量的片段;
bool IsValidFilename(string testName)
{
Regex containsABadCharacter = new Regex("["
+ Regex.Escape(new string(System.IO.Path.GetInvalidPathChars())) + "]");
if (containsABadCharacter.IsMatch(testName)) { return false; };
// other checks for UNC, drive-path format, etc
return true;
}
Run Code Online (Sandbox Code Playgroud)
一旦你知道了,你还应该检查不同的格式,例如c:\my\drive和\\server\share\dir\file.ext
小智 25
尝试使用它,并捕获错误.允许的集可能会跨文件系统或跨不同版本的Windows进行更改.换句话说,如果您想知道Windows是否喜欢该名称,请将其命名并让它告诉您.
Ste*_*per 23
该类清除文件名和路径; 用它就像
var myCleanPath = PathSanitizer.SanitizeFilename(myBadPath, ' ');
Run Code Online (Sandbox Code Playgroud)
这是代码;
/// <summary>
/// Cleans paths of invalid characters.
/// </summary>
public static class PathSanitizer
{
/// <summary>
/// The set of invalid filename characters, kept sorted for fast binary search
/// </summary>
private readonly static char[] invalidFilenameChars;
/// <summary>
/// The set of invalid path characters, kept sorted for fast binary search
/// </summary>
private readonly static char[] invalidPathChars;
static PathSanitizer()
{
// set up the two arrays -- sorted once for speed.
invalidFilenameChars = System.IO.Path.GetInvalidFileNameChars();
invalidPathChars = System.IO.Path.GetInvalidPathChars();
Array.Sort(invalidFilenameChars);
Array.Sort(invalidPathChars);
}
/// <summary>
/// Cleans a filename of invalid characters
/// </summary>
/// <param name="input">the string to clean</param>
/// <param name="errorChar">the character which replaces bad characters</param>
/// <returns></returns>
public static string SanitizeFilename(string input, char errorChar)
{
return Sanitize(input, invalidFilenameChars, errorChar);
}
/// <summary>
/// Cleans a path of invalid characters
/// </summary>
/// <param name="input">the string to clean</param>
/// <param name="errorChar">the character which replaces bad characters</param>
/// <returns></returns>
public static string SanitizePath(string input, char errorChar)
{
return Sanitize(input, invalidPathChars, errorChar);
}
/// <summary>
/// Cleans a string of invalid characters.
/// </summary>
/// <param name="input"></param>
/// <param name="invalidChars"></param>
/// <param name="errorChar"></param>
/// <returns></returns>
private static string Sanitize(string input, char[] invalidChars, char errorChar)
{
// null always sanitizes to null
if (input == null) { return null; }
StringBuilder result = new StringBuilder();
foreach (var characterToTest in input)
{
// we binary search for the character in the invalid set. This should be lightning fast.
if (Array.BinarySearch(invalidChars, characterToTest) >= 0)
{
// we found the character in the array of
result.Append(errorChar);
}
else
{
// the character was not found in invalid, so it is valid.
result.Append(characterToTest);
}
}
// we're done.
return result.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
Sco*_*man 22
这是我使用的:
public static bool IsValidFileName(this string expression, bool platformIndependent)
{
string sPattern = @"^(?!^(PRN|AUX|CLOCK\$|NUL|CON|COM\d|LPT\d|\..*)(\..+)?$)[^\x00-\x1f\\?*:\"";|/]+$";
if (platformIndependent)
{
sPattern = @"^(([a-zA-Z]:|\\)\\)?(((\.)|(\.\.)|([^\\/:\*\?""\|<>\. ](([^\\/:\*\?""\|<>\. ])|([^\\/:\*\?""\|<>]*[^\\/:\*\?""\|<>\. ]))?))\\)*[^\\/:\*\?""\|<>\. ](([^\\/:\*\?""\|<>\. ])|([^\\/:\*\?""\|<>]*[^\\/:\*\?""\|<>\. ]))?$";
}
return (Regex.IsMatch(expression, sPattern, RegexOptions.CultureInvariant));
}
Run Code Online (Sandbox Code Playgroud)
第一个模式创建一个正则表达式,其中仅包含Windows平台的无效/非法文件名和字符.第二个做同样的事情,但确保该名称对任何平台都是合法的.
Jon*_*der 18
记住一个角落的情况,当我第一次发现它时让我感到惊讶:Windows允许文件名中的前导空格字符!例如,以下是Windows上的所有合法且不同的文件名(减去引号):
"file.txt"
" file.txt"
" file.txt"
Run Code Online (Sandbox Code Playgroud)
从中可以看出:在编写用于修剪文件名字符串中的前导/尾随空格的代码时要小心.
简化Eugene Katz的答案:
bool IsFileNameCorrect(string fileName){
return !fileName.Any(f=>Path.GetInvalidFileNameChars().Contains(f))
}
Run Code Online (Sandbox Code Playgroud)
要么
bool IsFileNameCorrect(string fileName){
return fileName.All(f=>!Path.GetInvalidFileNameChars().Contains(f))
}
Run Code Online (Sandbox Code Playgroud)
Microsoft Windows:Windows内核禁止使用范围1-31(即0x01-0x1F)和字符"*:<>?\ |中的字符.虽然NTFS允许每个路径组件(目录或文件名)长度为255个字符,最长约32767个字符的路径,Windows内核仅支持长达259个字符的路径.此外,Windows禁止使用MS-DOS设备名称AUX,CLOCK $,COM1,COM2,COM3,COM4,COM5,COM6, COM7,COM8,COM9,CON,LPT1,LPT2,LPT3,LPT4,LPT5,LPT6,LPT7,LPT8,LPT9,NUL和PRN,以及带有任何扩展名的这些名称(例如,AUX.txt),使用时除外长UNC路径(例如\.\ C:\nul.txt或\?\ D:\ aux\con).(实际上,如果提供了扩展,则可以使用CLOCK $.)这些限制仅适用于Windows - 例如,Linux允许使用"*:<>?\ | 甚至在NTFS.
资料来源:http://en.wikipedia.org/wiki/Filename
问题是您是在尝试确定路径名是否是合法的Windows路径,或者它是否在运行代码的系统上是合法的.?我认为后者更重要,所以个人而言,我可能会分解完整路径并尝试使用_mkdir来创建文件所属的目录,然后尝试创建该文件.
这样,您不仅知道路径是否仅包含有效的窗口字符,而且它实际上是否表示此进程可以写入的路径.
我使用它来摆脱文件名中的无效字符而不抛出异常:
private static readonly Regex InvalidFileRegex = new Regex(
string.Format("[{0}]", Regex.Escape(@"<>:""/\|?*")));
public static string SanitizeFileName(string fileName)
{
return InvalidFileRegex.Replace(fileName, string.Empty);
}
Run Code Online (Sandbox Code Playgroud)