Verify if file exists or not in C#

后端 未结 12 1254
名媛妹妹
名媛妹妹 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.");
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-15 17:39

    You could use:

    System.IO.File.Exists(@"c:\temp\test.txt");
    
    0 讨论(0)
  • 2020-12-15 17:41

    Can't comment yet, but I just wanted to disagree/clarify with erikkallen.

    You should not just catch the exception in the situation you've described. If you KNEW that the file should be there and due to some exceptional case, it wasn't, then it would be acceptable to just attempt to access the file and catch any exception that occurs.

    In this case, however, you are receiving input from a user and have little reason to believe that the file exists. Here you should always use File.Exists().

    I know it is cliché, but you should only use Exceptions for an exceptional event, not as part as the normal flow of your application. It is expensive and makes code more difficult to read/follow.

    0 讨论(0)
  • 2020-12-15 17:42

    To test whether a file exists in .NET, you can use

    System.IO.File.Exists (String)
    
    0 讨论(0)
  • 2020-12-15 17:44

    I have written this code in vb and its is working fine to check weather a file is exists or not for fileupload control. try it

    FOR VB CODE ============

        If FileUpload1.HasFile = True Then
            Dim FileExtension As String = System.IO.Path.GetExtension(FileUpload1.FileName)
    
            If FileExtension.ToLower <> ".jpg" Then
                lblMessage.ForeColor = System.Drawing.Color.Red
                lblMessage.Text = "Please select .jpg image file to upload"
            Else
                Dim FileSize As Integer = FileUpload1.PostedFile.ContentLength
    
                If FileSize > 1048576 Then
                    lblMessage.ForeColor = System.Drawing.Color.Red
                    lblMessage.Text = "File size (1MB) exceeded"
                Else
                    Dim FileName As String = System.IO.Path.GetFileName(FileUpload1.FileName)
    
                    Dim ServerFileName As String = Server.MapPath("~/Images/Folder1/" + FileName)
    
                    If System.IO.File.Exists(ServerFileName) = False Then
                        FileUpload1.SaveAs(Server.MapPath("~/Images/Folder1/") + FileUpload1.FileName)
                        lblMessage.ForeColor = System.Drawing.Color.Green
                        lblMessage.Text = "File : " + FileUpload1.FileName + " uploaded successfully"
                    Else
                        lblMessage.ForeColor = System.Drawing.Color.Red
                        lblMessage.Text = "File : " + FileName.ToString() + " already exsist"
                    End If
                End If
            End If
        Else
            lblMessage.ForeColor = System.Drawing.Color.Red
            lblMessage.Text = "Please select a file to upload"
        End If
    

    FOR C# CODE ======================

    if (FileUpload1.HasFile == true) {
        string FileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);
    
        if (FileExtension.ToLower != ".jpg") {
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Text = "Please select .jpg image file to upload";
        } else {
            int FileSize = FileUpload1.PostedFile.ContentLength;
    
            if (FileSize > 1048576) {
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Text = "File size (1MB) exceeded";
            } else {
                string FileName = System.IO.Path.GetFileName(FileUpload1.FileName);
    
                string ServerFileName = Server.MapPath("~/Images/Folder1/" + FileName);
    
                if (System.IO.File.Exists(ServerFileName) == false) {
                    FileUpload1.SaveAs(Server.MapPath("~/Images/Folder1/") + FileUpload1.FileName);
                    lblMessage.ForeColor = System.Drawing.Color.Green;
                    lblMessage.Text = "File : " + FileUpload1.FileName + " uploaded successfully";
                } else {
                    lblMessage.ForeColor = System.Drawing.Color.Red;
                    lblMessage.Text = "File : " + FileName.ToString() + " already exsist";
                }
            }
        }
    } else {
        lblMessage.ForeColor = System.Drawing.Color.Red;
        lblMessage.Text = "Please select a file to upload";
    }
    
    0 讨论(0)
  • 2020-12-15 17:46

    Try this:

         string fileName = "6d294041-34d1-4c66-a04c-261a6d9aee17.jpeg";
    
         string deletePath= "/images/uploads/";
    
         if (!string.IsNullOrEmpty(fileName ))
            {
                // Append the name of the file to previous image.
                deletePath += fileName ;
    
                if (File.Exists(HttpContext.Current.Server.MapPath(deletePath)))
                {
                    // deletevprevious image
                    File.Delete(HttpContext.Current.Server.MapPath(deletePath));
                }
            }
    
    0 讨论(0)
提交回复
热议问题