Determine via C# whether a string is a valid file path

后端 未结 10 1802
情歌与酒
情歌与酒 2020-12-08 13:36

I would like to know how to determine whether string is valid file path.

The file path may or may not exist.

10条回答
  •  轮回少年
    2020-12-08 14:04

    You can simply use Path.Combine() inside a try catch statement:

    string path = @" your path ";
    try
    {
        Path.Combine(path);
    }
    catch
    {
        MessageBox.Show("Invalid path");
    }
    

    Edit: Note that this function doesn't throw an exception if path contains wildcard characters ('*' and '?') since they can be used in search strings.

提交回复
热议问题