如何检测项目文件夹中是否存在文件?

发布于 2024-09-14 04:01:26 字数 60 浏览 11 评论 0原文

我的项目文件夹中有一组图像。

如何检测我的项目文件夹中是否存在图像?我正在使用c#。谢谢。

I am having a collection of images in my project folder.

how to detect if a image exist in my project folder? I am using c#. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

内心激荡 2024-09-21 04:01:26
if (System.IO.File.Exists("pathtofile"))
  //it exist
else
  //it does not exist

在问题评论后编辑了我的答案:

我复制了代码并更改了退出函数,这应该可以工作

string type = Path.GetExtension(filepath); 
string path = @"image/" + type + ".png"; 
//if(System.IO.File.Exists(path)) I forgot to use the full path
if (System.IO.File.Exists(Path.Combine(Directory.GetCurrentDirectory(), path)))
 { return path; } 
else 
 { return @"image/other.png"; }

当您的应用程序部署时,这确实可以工作

if (System.IO.File.Exists("pathtofile"))
  //it exist
else
  //it does not exist

EDITED MY ANSWER AFTER THE COMMENT OF THE QUESTION:

I copied the code and changed the exits function, this should work

string type = Path.GetExtension(filepath); 
string path = @"image/" + type + ".png"; 
//if(System.IO.File.Exists(path)) I forgot to use the full path
if (System.IO.File.Exists(Path.Combine(Directory.GetCurrentDirectory(), path)))
 { return path; } 
else 
 { return @"image/other.png"; }

This will indeed work when your app is deployed

遮云壑 2024-09-21 04:01:26

这个问题有点不清楚,但我的印象是你在追求
exe安装的路径?

  class Program
  {
    static Dictionary<string, string> typeImages = null;

    static string GetImagePath(string type)
    {
      if (typeImages == null)
      {
        typeImages = new Dictionary<string, string>();
        string appPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
        string path = Path.Combine(appPath, @"image/");
        foreach (string file in Directory.GetFiles(path))
        {
          typeImages.Add(Path.GetFileNameWithoutExtension(file).ToUpper(), Path.GetFullPath(file));
        }
      }

      if (typeImages.ContainsKey(type))
        return typeImages[type];
      else
        return typeImages["OTHER"];
    }

    static void Main(string[] args)
    {
      Console.WriteLine("File for XLS="+GetImagePath("XLS"));
      Console.WriteLine("File for ZZZ=" + GetImagePath("ZZZ"));
      Console.ReadKey();
    }
  }

这将为您提供一个图像文件夹,该文件夹将位于安装 exe 的位置。
在开发环境中,您必须在应用程序路径中的 debug 和release 下创建一个图像目录,因为这是 VS 放置 exe 的位置。

The question is a little unclear but I get the impression that you're after
the path the exe has been installed in?

  class Program
  {
    static Dictionary<string, string> typeImages = null;

    static string GetImagePath(string type)
    {
      if (typeImages == null)
      {
        typeImages = new Dictionary<string, string>();
        string appPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
        string path = Path.Combine(appPath, @"image/");
        foreach (string file in Directory.GetFiles(path))
        {
          typeImages.Add(Path.GetFileNameWithoutExtension(file).ToUpper(), Path.GetFullPath(file));
        }
      }

      if (typeImages.ContainsKey(type))
        return typeImages[type];
      else
        return typeImages["OTHER"];
    }

    static void Main(string[] args)
    {
      Console.WriteLine("File for XLS="+GetImagePath("XLS"));
      Console.WriteLine("File for ZZZ=" + GetImagePath("ZZZ"));
      Console.ReadKey();
    }
  }

This will give you an image folder that will be wherever the exe is installed.
In the dev environment, you'll have to create an images dir under debug and release in the app path because that's where VS puts the exe's.

叹倦 2024-09-21 04:01:26

使用 File.Exists(Path Here) 如果您使用临时路径,请使用 Path.GetTempPath()

编辑:抱歉,答案与上面相同!

Use File.Exists(Path Here) If your using a temp path use Path.GetTempPath()

EDIT: Sorry, same answer as above!

你是暖光i 2024-09-21 04:01:26

您可以用来

string[] filenames = Directory.GetFiles(path);

获取文件夹中的文件列表,然后迭代它们,直到找到您要查找的内容(或找不到),

或者您可以尝试在 try catch 块中打开文件,如果出现异常,则意味着该文件不存在。

you could use

string[] filenames = Directory.GetFiles(path);

to get a list of the files in the folder and then iterate through them until you find what your looking for (or not)

or you could try to open the file in a try catch block and if you get an exception it means the file does not exist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文