Developer forum

Forum » CMS - Standard features » Checking if file exists returns false, altough I know it exists

Checking if file exists returns false, altough I know it exists

Hans Ravnsfjall
Hans Ravnsfjall
Reply

Hi

the file i am testing on has the path E:\dynamicweb.net\solutions\Sendistovan\lyfta.dw9.dynamicweb-cms.com\files\Images\1883.jpg

 

This is stored in a string named "ImgOnServer"

And the public link is http://lyfta.dw9.dynamicweb-cms.com/files/Images/1883.jpg

 

But this code returns false.

File.Exists(System.Web.HttpContext.Current.Server.MapPath("@ImgOnServer"))

 

Does it have something to do with permission, or am I doing something wrong in the code?

 

 

/Hans

 

 

 

 


Replies

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

The entire code is like this:

 

@{

string myndclean=@GetString("Ecom:Product.ImageSmall.Default.Clean");
string prodimgsolo = myndclean.Split('/').Last();
string ifolder = myndclean.Substring(0, myndclean.LastIndexOf("/")).ToString();
string ifolderOnDisk = System.Web.HttpContext.Current.Server.MapPath(ifolder);
DirectoryInfo idirectoryInfo = new System.IO.DirectoryInfo(ifolderOnDisk);
string ImgOnServer=@idirectoryInfo+"\\"+@prodimgsolo;


  }

@if(!String.IsNullOrEmpty(@GetString("Ecom:Product:Field.Mynd.Clean"))){
mynd=@GetString("Ecom:Product:Field.Mynd.Clean");
    }

  else if(File.Exists(System.Web.HttpContext.Current.Server.MapPath("@ImgOnServer")) == true){
    mynd=@myndclean;
}
else
  {
  mynd="/Files/Images/vorumyndmanglar@2x.jpg";
  }

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Apparently it helps to ask the question, because I found the error when creating this post :)

Problem was I was duplicating the "Server Path"

It should look like this:

 

@{

string myndclean=@GetString("Ecom:Product.ImageSmall.Default.Clean");
string prodimgsolo = myndclean.Split('/').Last();
string ifolder = myndclean.Substring(0, myndclean.LastIndexOf("/")).ToString();
string ifolderOnDisk = System.Web.HttpContext.Current.Server.MapPath(ifolder);
DirectoryInfo idirectoryInfo = new System.IO.DirectoryInfo(ifolderOnDisk);
string ImgOnServer=@idirectoryInfo+"\\"+@prodimgsolo;


  }

@if(!String.IsNullOrEmpty(@GetString("Ecom:Product:Field.Mynd.Clean"))){
mynd=@GetString("Ecom:Product:Field.Mynd.Clean");
    }

  else if(File.Exists(@ImgOnServer) == true){
    mynd=@myndclean;
}
else
  {
  mynd="/Files/Images/vorumyndmanglar@2x.jpg";
  }

 

You must be logged in to post in the forum