简体   繁体   English

检查控制台应用程序的文件夹中是否存在文件

[英]Check If file exists inside a folder of a Console Application

I have a console application project in C#, inside my project I have a Image folder. 我在C#中有一个控制台应用程序项目,在我的项目中我有一个Image文件夹。 I want to check if specific image exists inside that folder. 我想检查该文件夹中是否存在特定图像。 If I use AppDomain.CurrentDomain.BaseDirectory or Environment.CurrentDirectory I get the bin\\Debug folder. 如果我使用AppDomain.CurrentDomain.BaseDirectory或Environment.CurrentDirectory,我会得到bin \\ Debug文件夹。 How can I point to the Image folder? 我怎样才能指向Image文件夹? I want to make sure that this check will work when I deploy the project. 我希望确保在部署项目时此检查有效。

You get bin\\Debug because the .exe is copied to that path and run from there while running in the Visual Studio debugger. 你得到bin\\Debug因为.exe被复制到那个路径并在Visual Studio调试器中运行时从那里运行。 By default, image files are not copied to that folder. 默认情况下,图像文件不会复制到该文件夹​​。

If you go to the properties in Visual Studio of each image and set the Copy to Output Directory property to Copy if newer or Copy always, the images will be copied appropriately and you can open them with a relative path, eg "Images/My.png" 如果转到每个图像的Visual Studio属性并将“复制到输出目录”属性设置为“复制为更新”或“始终复制”,则图像将被正确复制,您可以使用相对路径打开它们,例如“Images / My”。 PNG”

You can set your images Copy to Output Directory property to Copy Always / Copy if newer 您可以将图像复制到输出目录属性设置为复制始终/复制(如果较新)

在此输入图像描述

With this, it will be copied to the folder where the executing application resides (bin\\Debug). 这样,它将被复制到正在执行的应用程序所在的文件夹(bin \\ Debug)。 And then you could just reference the image path 然后你可以参考图像路径

string imagePath = Path.Combine(Environment.CurrentDirectory, "Images");

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

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