Check if a file/directory exists: is there a better way?

前端 未结 8 1960
情歌与酒
情歌与酒 2020-12-09 07:41

I find myself doing this a lot just to ensure the filename is not in use. Is there a better way?

Directory.Exists(name) || File.Exists(name)
8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-09 08:08

    You can use following function:

    [DllImport("shlwapi", EntryPoint = "PathFileExists", CharSet = CharSet.Unicode)]
    public static extern bool PathExists(string path);
    

提交回复
热议问题