在C#检查文件名是* *可能有效(没有它的存在) [英] In C# check that filename is *possibly* valid (not that it exists)
本文介绍了在C#检查文件名是* *可能有效(没有它的存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有在System.IO命名空间的方法来检查文件名的有效性?
Is there a method in the System.IO namespace that checks the validity of a filename?
例如, C:\\富\\栏
将验证和:〜 - *
不会
还是有点麻烦, X:\\富\\栏
将验证是有一个 X:
驾驶该系统,但不会以其他方式。
Or a little trickier, X:\foo\bar
would validate is there is an X:
drive on the system, but wouldn't otherwise.
我想我可以写这样的方法我自己,但我更感兴趣的是内置的。
I suppose I could write such a method myself, but I'm more interested in a built-in one.
推荐答案
只是做
System.IO.FileInfo fi = null;
try
{
fi = new System.IO.FileInfo(fileName);
}
catch (ArgumentException) { }
catch (System.IO.PathTooLongException) { }
catch (NotSupportedException) { }
if (ReferenceEquals(fi, null))
{
...
// file name is not valid
}
else
{
...
// file name is valid... May check for existence by calling fi.Exists.
}
有关创建的FileInfo
实例的文件不需要存在。
For creating a FileInfo
instance the file does not need to exist.
这篇关于在C#检查文件名是* *可能有效(没有它的存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文