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

后端 未结 10 1793
情歌与酒
情歌与酒 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 13:59

    You can't really be sure until you try to create that file. Maybe the path is valid but the security settings won't allow creation of the file. The only instance that could tell you if the path is REALLY valid would be the OS, so why don't you try to create that file an catch the IOException which indicates it's not valid? In my humble opinion, this is an approach: Assume the input is valid, use it, and catch an IOException when it isn't valid.

提交回复
热议问题