tags:

views:

75

answers:

4

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.

+6  A: 
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

Wouter Janssens - Xelos bvba
An alternative is to use `FileInfo`, if you need to also get timestamps and other basic information.
Steven Sudit
@Steven: that is correct if you want the info of the file but File.Exists has a better performance if you only need to know if the exist
Wouter Janssens - Xelos bvba
Yes, that's why I suggested it as an alternative if you're also going to need additional information, not as a general replacement.
Steven Sudit
@VHanded: Please mark the answer as Accepted if this answer helped you. Then the answer gets marked and other people can see that the problem is solved
Wouter Janssens - Xelos bvba
Sorry maybe my question is not clear enough, but this is not what I asked for. I just updated my question in my comment in my question, but the editor cramped all together.
VHanded
I edited my answer to your update, I hope this is clear enough
Wouter Janssens - Xelos bvba
Won't the path be relative to the current directory the application is started from?
JonB
ok sorry my mistake, it must be Path.Combine(Directory.GetCurrentDirectory(), path), I will edit my answer again
Wouter Janssens - Xelos bvba
It works! Thanks! Love this community~
VHanded
No problem liked to help you.
Wouter Janssens - Xelos bvba
Sorry, I tested it, and realize that the GetCurrentDirectory() is actually return the bin directory. The image folder is not located in the bin directory. Any workaround??
VHanded
You can use ..\ or ../ to go 1 folder up in the structure. example: string path = Path.Combine(Directory.GetCurrentDirectory(), @"../image/" + type + ".png"; if (System.IO.File.Exists(path)) ...
Wouter Janssens - Xelos bvba
is this working for you or not?
Wouter Janssens - Xelos bvba
A: 

Hi

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.

Constantin
These are not good ideas.
Steven Sudit
Not as efficient as the `File.Exists` or `FileInfo.Exists` methods.
tdammers
Is it really less efficient? You'd use it in a lazy evaluation style or get the files on startup and keep the list.
JonB
A: 

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

EDIT: Sorry, same answer as above!

Vibralux
A: 

The question is a little unclear but I get the impression that your 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.

JonB