mathinvalidnik
mathinvalidnik

Reputation: 1600

How to check if file exists in a folder in my project in Visual Studio solution

I have a folder created in my Project. In my code I want to determine if a file exists in that folder. How can I do that ?

---------- EDIT:

I'll try to put the question in a different way.

I have an mvc 4 project that has all of the startup folders : Controllers, Views, Models etc. I have added another folder in my project called MyResources . In that folder I have added few pdf files. In one of my controllers i have a logics that has to check if the passed name of file exists in that particular folder. Lets say that I have passed PassedFileName.pd and I want to check if that file is available in the MyResources folder. I have tried with the System.IO.File.Exists(@"~/MyResources/PassedFileName.pdf") but it always returns false. When I right-click on the file itself( in the solution explorer) and see what is its actual path it says: C:\(phisical-path-on-my-machine)\MyProjectName\Resources\ReturnHelpPdf.pdf . That makes me think that I need the path to my project somehow so I can string.Format it. I hope that you understand what are my concerns. I know how to check if file exists on the File system. But here I have to make check for something I am not complete sure if I have the full information about.

Upvotes: 1

Views: 17028

Answers (7)

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

Problem : you need to provide the valid physical path of the file. to check with File.Exists() method.

Solution : you need to use the Server.MapPath() function to get the valid physical Path of the given relative path.

Try This:

 String path=Server.MapPath(@"~/MyResources/PassedFileName.pdf");
 if(File.Exists(path))
 {
  //File Found

 }

Upvotes: 3

Scott Chamberlain
Scott Chamberlain

Reputation: 127543

To resolve the ~\ path you need to use the function HttpServerUtility.MapPath.

System.IO.File.Exists(HttpServerUtility.MapPath(@"~/MyResources/PassedFileName.pdf"))

What MapPath will do is turn the ~\ in to the path that your project is currently running on the IIS server.

Upvotes: 1

Kurubaran
Kurubaran

Reputation: 8902

If you alredy know the file name then use

if(File.Exist("File path"))
{
}

If you want to check if any file exist within that folder then use

string[] files = Directory.GetFiles("Directory Path");
if(files.Length > 0)
{
//File exist
}

Upvotes: 0

Paulo Correia
Paulo Correia

Reputation: 616

Usualy Folders inside a Visual Studio Solution, are for organization purpose, and they don't exist in the file system. Since the Visual Studio Solution file has XML on it, you can load the file and with XPath, find the location of the folder, and if it has any files in it.

You can create only the solution, a folder, and add a file to it, and check inside the XML how Visual Studio store that information.

Upvotes: 0

Kane
Kane

Reputation: 16802

Programatically you can use the System.IO.File.Exists() or System.IO.Directory.Exists() methods

Upvotes: 1

Shyju
Shyju

Reputation: 218722

You can use File.Exists method to check a file exists on a path. This is available in the System.IO namespace.

string filePath= @"c:\Projects\sample.txt";
if(File.Exists(filePath))
{
  //Do something
}

Upvotes: 2

xeraphim
xeraphim

Reputation: 4645

You can use var fileExists = File.Exists(path); to check if a file exists at a given path if the file exists, the variable fileExists will be true else it is false.

Of course you can also check directly in a if statement

if(File.Exists(path))
{
    ....
}
else { ... }

Upvotes: 4

Related Questions