C# 使用相对路径检查文件是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12941908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking the existence of a file using relative path
提问by logeeks
What is the best way to check the existence of a file using relative path.
使用相对路径检查文件是否存在的最佳方法是什么。
I've used the following method but it returns false despite the fact that file is existing.
我使用了以下方法,但尽管文件存在,但它返回 false。
bool a = File.Exists("/images/Customswipe_a.png");
采纳答案by Mike Park
That's not a relative path. You need to leave off the first /otherwise it will be interpreted as being rooted (i.e. C:/images...)
那不是相对路径。你需要去掉第一个,/否则它会被解释为 root (即 C:/images...)
回答by Codeman
The path is relative to the location of your binary file. In the case of a visual studio project, this would be %PROJECTDIR%/bin/(RELEASE||DEBUG)/
该路径相对于二进制文件的位置。在视觉工作室项目的情况下,这将是%PROJECTDIR%/bin/(RELEASE||DEBUG)/
What I would do is put the filesystem root in a config file, and use that for your relative path.
我要做的是将文件系统根目录放在一个配置文件中,并将其用于您的相对路径。
回答by jac
The relative path is relative to the current working directory. It may not be the application directory. Call GetCurrentDirectory()to check the actual path you are testing.
相对路径是相对于当前工作目录的。它可能不是应用程序目录。调用GetCurrentDirectory()来检查您正在测试的实际路径。
回答by Tigran
The relative path, is a relativeto something. In this API, it will, according to the documentation File.Exists:
相对路径,是相对于某物的。在这个 API 中,它会根据文档File.Exists:
Relative path information is interpreted as relative to the current working directory.
相对路径信息被解释为相对于当前工作目录。
So everything here is depends what is CurrentDirectotyat the moment of execution of this query.
所以这里的一切都取决于执行此查询时CurrentDirectoty是什么。
Plus, your path is not valid Desktop path (I assume you pick it from some web file, or knowledge). To understand if specified path contains not valid characters use GetInvalidCharactersfunction.
另外,您的路径不是有效的桌面路径(我假设您是从某个 Web 文件或知识中选择的)。要了解指定的路径是否包含无效字符,请使用GetInvalidCharacters函数。
In your specific case it would be enough to use @"\images\Customswipe_a.png".
在您的特定情况下,使用@"\images\Customswipe_a.png".
回答by pkmiec
I guess that you are running this code in asp.net application, thats why you get false.
我猜您是在 asp.net 应用程序中运行此代码,这就是您得到错误的原因。
In asp.net you should use Server.MapPath("/images/Customswipe_a.png")to get "correct" path (relative to the web application root directory). Otherwise you get path local to the webserver executable (IIS/WEBDAV/..name any other).
在 asp.net 中,您应该使用Server.MapPath("/images/Customswipe_a.png")获取“正确”路径(相对于 Web 应用程序根目录)。否则,您将获得网络服务器可执行文件的本地路径(IIS/WEBDAV/..name 任何其他)。
回答by Olivier Jacot-Descombes
In a WinForms application you can get the directory of the exe file with
在 WinForms 应用程序中,您可以使用以下命令获取 exe 文件的目录
string directory =
Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
Another solution uses Reflection
另一种解决方案使用反射
string directory =
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
回答by zahir
You just need to define what your file is relative to
你只需要定义你的文件相对于什么
- Your application main assembly?
- Current directory?
- Application data directory?
- name it...
- 您的应用程序主程序集?
- 当前目录?
- 应用数据目录?
- 命名...
In each of these cases I'd suggest you to convert it into an absolute path by Path.Combinemethod:
在每种情况下,我建议您通过Path.Combine方法将其转换为绝对路径:
public static readonly string AppRoot = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
...
//calling with a '/' heading makes the path absolute so I removed it
var fullPath = Path.Combine(AppRoot, "images/Customswipe_a.png");
var exists = File.Exists(fullPath);
This way you can guarantee where you are looking for. Even the Open/Save file dialogs may change your current directory. So, calling File.Existswithout full path is usually a wrong decision.
这样您就可以保证您正在寻找的位置。甚至打开/保存文件对话框也可能更改您的当前目录。因此,File.Exists没有完整路径的调用通常是一个错误的决定。
回答by FreeClimb
You can test this path with System.IO.DirectoryInfo:
您可以使用 System.IO.DirectoryInfo 测试此路径:
DirectoryInfo info = new DirectoryInfo("/images/Customswipe_a.png");
string absoluteFullPath = info.FullName;
As Mike Park correctly answered this path is likely to be (i.e. C:/images...)
由于 Mike Park 正确回答了这条路径很可能是(即 C:/images...)
回答by Andrew
As of .NET Core 3.1, this works:
从.NET Core 3.1 开始,这有效:
Use dependency injection to include service IWebHostEnvironment
使用依赖注入来包含服务 IWebHostEnvironment
private IWebHostEnvironment Env;
public MyController (IWebHostEnvironment env){
Env = env;
}
Then use it to get path to root of app with Env.ContentRootPath
然后使用它来获取应用程序根目录的路径 Env.ContentRootPath
And combine it all:
并将其全部结合起来:
var file = System.IO.Path.Combine(Env.ContentRootPath, "images", "some-file.png");

