简体   繁体   English

如何在Visual Studio解决方案的项目中检查文件是否存在于文件夹中

[英]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 . 我有一个包含所有启动文件夹的mvc 4项目:Controllers,Views,Models等。我在项目中添加了另一个文件夹MyResources In that folder I have added few pdf files. 在该文件夹中,我添加了一些pdf文件。 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. 可以说我已经传递了PassedFileName.pd ,我想检查MyResources文件夹中该文件是否可用。 I have tried with the System.IO.File.Exists(@"~/MyResources/PassedFileName.pdf") but it always returns false . 我尝试使用System.IO.File.Exists(@"~/MyResources/PassedFileName.pdf")但它始终返回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 . 当我右键单击文件本身(在解决方案资源管理器中)并查看其实际路径是什么时: 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. 这让我认为我需要以某种方式到达我的项目的路径,以便我可以进行string.Format 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); 您可以使用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 . 要检查文件是否存在于给定路径中(如果文件存在),则变量fileExists将为true否则为false

Of course you can also check directly in a if statement 当然,您也可以直接在if语句中检查

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

You can use File.Exists method to check a file exists on a path. 您可以使用File.Exists方法检查路径上是否存在文件。 This is available in the System.IO namespace. System.IO命名空间中可用。

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

Problem : you need to provide the valid physical path of the file. 问题:您需要提供文件的有效physical path to check with File.Exists() method. 使用File.Exists()方法进行检查。

Solution : you need to use the Server.MapPath() function to get the valid physical Path of the given relative path . 解决方案:您需要使用Server.MapPath()函数来获取给定relative path的有效physical Path 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. Visual Studio解决方案中的“通常”文件夹用于组织目的,并且在文件系统中不存在。 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. 由于Visual Studio解决方案文件上具有XML,因此可以加载该文件并使用XPath查找文件夹的位置,以及其中是否包含任何文件。

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. 您只能创建解决方案,文件夹,并向其中添加文件,然后在XML内部检查Visual Studio如何存储该信息。

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 . 要解析~\\路径,您需要使用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. MapPath作用是将~\\转到项目在IIS服务器上当前正在运行的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 拒绝访问路径“(临时文件夹)”。 将项目或类文件添加到我的Visual Studio解决方案时发生 - Access to the path '(Temp Folder)' is denied. Happens when adding a project or class file to my Visual Studio solution 将解决方案文件夹添加到visual studio项目模板 - Add solution folder to visual studio project template 项目级别的Visual Studio解决方案文件夹 - Visual studio solution folder at the project level 在Visual Studio项目模板中创建解决方案文件夹 - Create a Solution Folder in a Visual Studio Project Template 如何在Visual Studio中使用C#在解决方案中轻松获取文件夹内文件的路径? - How can I easily get the path of a file inside a folder in my solution in Visual Studio with C#? 如何从 Visual Studio 解决方案/项目中读取文件? - How to read file from Visual Studio solution/project? Visual Studio 2017 找不到解决方案中存在的文件夹 - Visual Studio 2017 can't find folder that exists in the solution 如何检查文件夹中是否存在文件? - How to check if a file exists in a folder? 如何从Visual Studio中的项目/服务器文件夹中获取文件名? - how to get file names from project/server folder in visual studio? 如何从 Visual Studio 项目中排除文件/文件夹? - How to exclude file/folder from visual studio project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM