简体   繁体   中英

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.

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 { ... }

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
}

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

 }

通过编程,您可以使用System.IO.File.Exists()System.IO.Directory.Exists()方法

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.

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
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM