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

8 浏览
0 Comments

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

我正在开发一个应用程序。该应用程序应该获得用户的简历,所以我需要一个代码来验证文件是否存在。

我正在使用ASP.NET/C#。

admin 更改状态以发布 2023年5月24日
0
0 Comments

为了测试.NET中是否存在一个文件,可以使用

System.IO.File.Exists (String)

0
0 Comments

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

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.");
        }
    }
}

0