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

前端 未结 8 1941
情歌与酒
情歌与酒 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条回答
  •  不思量自难忘°
    2020-12-09 07:55

    bool FileOrDirectoryExists(string path)
    {
        try
        {
            File.GetAttributes(_source);
        }
        catch (FileNotFoundException)
        {
            return false;
        }
        return true;
    }
    

提交回复
热议问题