C# 判断文件是否存在 - 使用 File.Exists() 方法
在 C# 中,可以使用 "File.Exists()" 方法来判断文件是否存在。该方法接受一个文件路径作为参数,并返回一个布尔值,表示文件是否存在。\n\n以下是一个示例代码:\n\ncsharp\nusing System;\nusing System.IO;\n\nclass Program\n{\n static void Main()\n {\n string filePath = \"C:\example.txt\";\n\n if (File.Exists(filePath))\n {\n Console.WriteLine(\"文件存在\");\n }\n else\n {\n Console.WriteLine(\"文件不存在\");\n }\n }\n}\n\n\n在上述示例中,我们通过 "File.Exists()" 方法判断了文件 "C:\example.txt" 是否存在,并根据结果输出不同的消息。
原文地址: https://www.cveoy.top/t/topic/qbr5 著作权归作者所有。请勿转载和采集!