How do I check if a given string is a legal/valid file name under Windows?

后端 未结 27 1904
-上瘾入骨i
-上瘾入骨i 2020-11-22 09:38

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 n

27条回答
  •  佛祖请我去吃肉
    2020-11-22 10:18

    This is an already answered question, but just for the sake of "Other options", here's a non-ideal one:

    (non-ideal because using Exceptions as flow control is a "Bad Thing", generally)

    public static bool IsLegalFilename(string name)
    {
        try 
        {
            var fileInfo = new FileInfo(name);
            return true;
        }
        catch
        {
            return false;
        }
    }
    

提交回复
热议问题