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

后端 未结 27 1711
-上瘾入骨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:11

    I got this idea from someone. - don't know who. Let the OS do the heavy lifting.

    public bool IsPathFileNameGood(string fname)
    {
        bool rc = Constants.Fail;
        try
        {
            this._stream = new StreamWriter(fname, true);
            rc = Constants.Pass;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Problem opening file");
            rc = Constants.Fail;
        }
        return rc;
    }
    

提交回复
热议问题