Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verify if file exists or not in C#

People also ask

How do you check file is exist or not?

To check if a file exists, you pass the file path to the exists() function from the os. path standard library. If the file exists, the exists() function returns True . Otherwise, it returns False .

How check file is present or not in C++?

Use ifile. open(): ifile. open() is mainly used to check if a file exists in the specific directory or not.

How do you know if a file is a directory in C?

The isDir() function is used to check a given file is a directory or not.


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

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

System.IO.File.Exists (String)

    if (File.Exists(Server.MapPath("~/Images/associates/" + Html.DisplayFor(modelItem => item.AssociateImage)))) 
      { 
        <img src="~/Images/associates/@Html.DisplayFor(modelItem => item.AssociateImage)"> 
      }
        else 
      { 
        <h5>No image available</h5> 
      }

I did something like this for checking to see if an image existed before displaying it.


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

Simple answer is that you can't - you won't be able to check a for a file on their machine from an ASP website, as to do so would be a dangerous risk for them.

You have to give them a file upload control - and there's not much you can do with that control. For security reasons javascript can't really touch it.

<asp:FileUpload ID="FileUpload1" runat="server" />

They then pick a file to upload, and you have to deal with any empty file that they might send up server side.


You could use:

System.IO.File.Exists(@"c:\temp\test.txt");

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.