且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

验证是否文件在C#中的存在与否

更新时间:2023-11-27 16:01:52

您可以判断是否存在指定的文件中使用的存在方法文件类中的 System.IO 命名空间:

You can determine whether a specified file exists using the Exists method of the File class in the System.IO namespace:

bool System.IO.File.Exists(string path)

您可以在这里找到文档在MSDN上

示例:

using System;
using System.IO;

class Test
{
    public static void Main()
    {
        string resumeFile = @"c:\ResumesArchive\923823.txt";
        string newFile = @"c:\ResumesImport\newResume.txt";
        if (File.Exists(resumeFile))
        {
            File.Copy(resumeFile, newFile);
        }
        else
        {
            Console.WriteLine("Resume file does not exist.");
        }
    }
}