0

I have a windows application with an "images" folder. I need to check if an image exists, which it will, during runtime. The below code is what I have but it always returns false.

 if ( File.Exists("images/" + item.tool_image) )
        {
            Image img;
            img = Image.FromFile("images/" + item.tool_image);
            titem.Image = img;
        }

Whats the problem or the proper way to do this.

Rob
  • 26,989
  • 16
  • 82
  • 98
mattgcon
  • 4,768
  • 19
  • 69
  • 117
  • path mentioned in exist is incorrect . Use full path of image folder. If the floder is on local machine use appdomain.basepath / "image/" + variable – Vikram Shetty Nov 13 '11 at 05:29
  • You should use the Path.Combine method when getting paths to files to avoid putting slashes in your strings. – JohnZaj Nov 13 '11 at 05:34
  • possible duplicate of [When is it okay to check if a file exists?](http://stackoverflow.com/questions/673654/when-is-it-okay-to-check-if-a-file-exists) – Joel Coehoorn Dec 13 '11 at 19:24

6 Answers6

3

If the file you're looking for doesn't exist in the working directory of your application, call File.Exists with a fully-qualified path:

if (File.Exists(@"C:\images\" + item.tool_image))
{ ... }

Of course, verify that a file actually exists at that location.

You'll find life easier if you use the tools provided by the Path class:

if (File.Exists(Path.Combine(@"C:\images", item.tool_image)))
{ ... }
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
  • Need to use `@"..."` verbatim string or escape the backslash `"\\"`. – bobbymcr Nov 13 '11 at 05:33
  • you probably want to escape the backslash character, or use the '@' sign to indicate an unescaped string. also, there's an extra quote at the end. – Jason Nov 13 '11 at 05:34
1

The path is wrong try to change it to

 string basePath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
            string imageFileName = System.IO.Path.Combine(basePath, "Images",item.tool_image);
     if ( File.Exists(imageFileName) )
            {
                Image img;
                img = Image.FromFile(imageFileName);
                titem.Image = img;
            }
DeveloperX
  • 4,633
  • 17
  • 22
1

How to properly use File.Exists in windows application?

You don't!

It's almost never appropriate to check if a file exists before trying to open. There are other things at work here: permissions, locking, sharing, time.

Instead, the correct way to do this is to try to open the file, whether it exists or not, and then catch the exception if your attempt to open the file fails. You have to be able to handle this exception anyway, even after performing the File.Exists() check. This makes your initial File.Exists() check not only redundant to your code, but wasteful, because it causes an extra trip out to the file system... and there's not much you can do in programming that's slower than going to the file system.

Community
  • 1
  • 1
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • serious? isn't the goal of efficient programming to make less error's when running an application then forcibly causing them? – mattgcon Dec 13 '11 at 18:21
  • @mattgcon - read my edited answer; maybe it will explain my reasoning a little better. In a nutshell, using File.Exists() is less efficient, both from a program performance standpoint and developer time/amount of code standpoint – Joel Coehoorn Dec 14 '11 at 02:05
  • I'm upvoting this as you're correct. File.Exists will not absolutely tell you if a file does not exist, only if a file does. Do not assume that because File.Exists returns false, that you're able to call File.Create as you may up with an IOException (file already exists, or some other error) or worse; overwrite a file that's already there (depending on FileOptions passed in). – Brent Newbury Nov 28 '12 at 12:19
0

it is looking from the location where the code is currently running, also the '/' is the wrong direction. also, you are defining the path in multiple places, which can lead to problems later.

var path = string.Format(@"c:\somewhere\images\{0}", item.tool_image);
if (File.Exists(path))
{
   Image img;
   img = Image.FromFile(path);
   titem.Image = img;
}

it's up to you to set the variable path , but in all likelihood, in your code example the location you expect isn't being checked.

Jason
  • 15,915
  • 3
  • 48
  • 72
0

The way you're calling it, you are looking for a file of whatever is in the string item.tool_image inside the images folder. Note that this images folder is located inside whatever directory contains your executable.

For instance, i just called File.Exists("images/image.jpg") and it worked.

aleph_null
  • 5,766
  • 2
  • 24
  • 39
0

As everyone has mentioned, use the fully qualified path. I also make heavy use of the Path.Combine, so I don't have to worry about missing a slash or two when I'm combining directories. The current executing directory is also useful...

File.Exists(Path.Combine(Environment.CurrentDirectory, "Images", item.tool_image));
Daryl
  • 18,592
  • 9
  • 78
  • 145