我可以使用os.path.exists()來檢查文件是否存在與否。 C#中的等效函數是什麼?C#等價於Python的os.path.exists()?
2
A
回答
3
System.IO.File
和System.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");
3
當然,你的意思是.NET,不C#:)
相關問題
- 1. C#等價於python的struct.pack
- 2. C++等價於Python的doctests?
- 3. Python等價於C#的選擇?
- 4. Python等價於ignoreboth:erasedups
- 5. 等價於C++中的C++
- 6. C++等價於Tidy
- 7. C++等價於SerializeWithLengthPrefix
- 8. Python等價於Mathematica的ArrayPlot?
- 9. PHP等價於Python的requests.get
- 10. Python等價於Mathematica的「LaguerreL」
- 11. python等價於ruby的__method__?
- 12. Ruby等價於Python的DictWriter?
- 13. python等價於ruby的`map.with_index`?
- 14. 等價於python「dir」的Java?
- 15. python等價於ruby的StringScanner?
- 16. python等價的C++ getline()
- 17. C#等價於C++ mem_fun?
- 18. Java的readInt()等價於C?
- 19. PHP的preg_match()等價於C++?
- 20. 等價於Python的ObjectSpace的Python?
- 21. R:+ =(加上等於)和++(加上)等價於C++/c#/ java等等?
- 22. Python等價於curl -b(--cookie)
- 23. VB.NET等價於C#代碼
- 24. QPainter等價於Objective C
- 25. Sscanf等價於Objective-C
- 26. C#等價於file_get_contents(PHP)
- 27. C linux等價於windows QueryPerformanceCounter
- 28. C#等價於imbue和numpunct
- 29. Perl語句等價於C
- 30. Objective-C等價於C#中的「override」#
Duplicate:http://stackoverflow.com/questions/38960/how-to-find-out-if-a-file-exists-in-c-net – 2011-02-11 17:28:07