Verify if file exists or not in C#

后端 未结 12 1391
名媛妹妹
名媛妹妹 2020-12-15 17:19

I am working on an application. That application should get the resume from the users, so that I need a code to verify whether a file exists or not.

I\'m using ASP.N

12条回答
  •  没有蜡笔的小新
    2020-12-15 17:36

    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)
    

    You can find the documentation here on MSDN.

    Example:

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

提交回复
热议问题