2011-02-11 101 views
2

我可以使用os.path.exists()來檢查文件是否存在與否。 C#中的等效函數是什麼?C#等價於Python的os.path.exists()?

+0

Duplicate:http://stackoverflow.com/questions/38960/how-to-find-out-if-a-file-exists-in-c-net – 2011-02-11 17:28:07

回答

3

System.IO.FileSystem.IO.Directory都有Exists

bool dirExists = System.IO.Directory.Exists(@"C:\directory\"); 
bool fileExists = System.IO.File.Exists(@"C:\directory\file.txt");

而且額外獎金:請注意,對於跨平臺的兼容性,應使用例如System.IO.Path.Combine("c:", "directory", "file.txt");。這將使用System.IO.Path.DirectorySeparatorChar自動加入目錄的各個部分。當然,只有Windows有C :,所以你需要知道該用什麼作爲驅動器的根目錄。

2
System.IO.File.Exists(@"c:\path\to\your\file.ext");