今天遇到如标题的错误,示例代码如下:
注:其中在第一行字符aaa后面有一个空格。于是第三句代码便会上述错误。用Reflector查看DirectoryInfo的GetFiles代码,发现在是调用NormalizePathFast中
Code
//string构造函数
static String()
{
Empty = "";
WhitespaceChars = new char[] {
'\t', '\n', '\v', '\f', '\r', ' ', '\x0085', '\x00a0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', '', '\u2028', '\u2029', ' ', ''
};
}
//TrimEnd
public string TrimEnd(params char[] trimChars)
{
if ((trimChars == null) || (trimChars.Length == 0))
{
trimChars = WhitespaceChars;
}
return this.TrimHelper(trimChars, 1);
}
原来最终在这里把字符串中空格给过滤了。而操作系统是允许建立名称最后是空格的文件夹的。于是再调用GetFiles方法时,原来的文件名已经变了,把空格过滤了。于是便出现了这个错误。
解决方法:
1,建立没有空白字符的文件夹名,但这个与操作系统冲突。
2,在使用文件夹名时加上"\",如
还有更好的方法吗?????
string
dirFullName
=
@"
E:\aaa
"
;
DirectoryInfo dir = new DirectoryInfo(dirFullName);
FileInfo[] fs = dir.GetFiles();
DirectoryInfo dir = new DirectoryInfo(dirFullName);
FileInfo[] fs = dir.GetFiles();
注:其中在第一行字符aaa后面有一个空格。于是第三句代码便会上述错误。用Reflector查看DirectoryInfo的GetFiles代码,发现在是调用NormalizePathFast中
if
(fullCheck)
{
path = path.TrimEnd( new char [ 0 ]);
CheckInvalidPathChars(path);
}
第三行出了错,又查了一下string的TrimEnd方法如下:
{
path = path.TrimEnd( new char [ 0 ]);
CheckInvalidPathChars(path);
}


//string构造函数
static String()
{
Empty = "";
WhitespaceChars = new char[] {
'\t', '\n', '\v', '\f', '\r', ' ', '\x0085', '\x00a0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', '', '\u2028', '\u2029', ' ', ''
};
}
//TrimEnd
public string TrimEnd(params char[] trimChars)
{
if ((trimChars == null) || (trimChars.Length == 0))
{
trimChars = WhitespaceChars;
}
return this.TrimHelper(trimChars, 1);
}
原来最终在这里把字符串中空格给过滤了。而操作系统是允许建立名称最后是空格的文件夹的。于是再调用GetFiles方法时,原来的文件名已经变了,把空格过滤了。于是便出现了这个错误。
解决方法:
1,建立没有空白字符的文件夹名,但这个与操作系统冲突。
2,在使用文件夹名时加上"\",如
string
dirFullName
=
@"
E:\aaa \
"
;
但这样也会增加操作。。
还有更好的方法吗?????