0

I have an image "1.png" inside "Images" directory. In css, I am referring image as "../../styles/ImagesNew/1.png", but this folder structure "styles/ImagesNew" does not even exists in my solution. I need to check, if "1.png" exists inside "styles/ImagesNew" folder, which it does not.

I dont want to do anything with this file. if the file does not exists inside the directory, escape from the if loop.

Just for more info, I am storing "1.png" in a string variable filename and "styles/ImagesNew/1.png" in a string variable foldername.

I searched a lot on internet, but i was unable to find "if a particular file exists inside a specific folder".

Rob
  • 26,989
  • 16
  • 82
  • 98
Kabir
  • 99
  • 1
  • 11
  • possible duplicate of [How to check if a specific file exists in directory or any of its subdirectories](http://stackoverflow.com/questions/3994448/how-to-check-if-a-specific-file-exists-in-directory-or-any-of-its-subdirectories) – Dhaust May 26 '15 at 22:21

1 Answers1

2

You are trying to get the file from virtual path, first convert it to physical path using Server.MapPath, you can read more about web project paths here.

if(File.Exists(Server.MapPath("~/styles/ImagesNew/FileName.Ext")))
{

}

ASP.NET includes the Web application root operator (~), which you can use when specifying a path in server controls. ASP.NET resolves the ~ operator to the root of the current application. You can use the ~ operator in conjunction with folders to specify a path that is based on the current root, MSDN

Adil
  • 146,340
  • 25
  • 209
  • 204
  • Adil, Yes I am using incorrect reference. I know that. I just want to check, if file exists inside that directory. thats it. In your solution, how would it check the filename "1.png" It will be ../../ and not ~ If you can please help me in finding this particular case. – Kabir Feb 21 '14 at 07:58
  • if(File.Exists(Server.MapPath("../../FileName.Ext"))), Make sure you get right path, you can check what you get by putting Server.MapPath("../../FileName.Ext") in some variable. – Adil Feb 21 '14 at 09:02
  • No. I am getting entire path of my application appended by "styles/ImagesNew/FileName.Ext" as "C:\\UserContextTestingApp\\UseContextApp\\styles\\ImagesNew\\FileName.Ext" Do I need to explain my question in detail. I just want to check, if a particular file (filename) exists in particular folder (foldername). – Kabir Feb 21 '14 at 13:35