I need to check for existence of a file on our website in "images"...

  • Thread starter Thread starter trint
  • Start date Start date
T

trint

How can I do this with my c# code with my website(because the file is
there, but the code doesn't return it)?:

if(File.Exists(String.Format("~/images/categories/{0}", sFileName))
return String.Format("~/images/categories/{0}", sFileName);
else if(File.Exists(String.Format("~/images/products/{0}",
sFileName))
return String.Format("~/images/products/{0}", sFileName);
else
return "";

I thought I might have to use:
Server.MapPath.

Any help is appreciated.
Thanks,
Trint
 
Trint,

Yes, you will have to use the MapPath method to get a path on the local
machine and then you can use the Exists method on the File class to check
the existence of it.

Hope this helps.
 
Trint,

Yes, you will have to use the MapPath method to get a path on the local
machine and then you can use the Exists method on the File class to check
the existence of it.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




How can I do this with my c# code with my website(because the file is
there, but the code doesn't return it)?:
if(File.Exists(String.Format("~/images/categories/{0}", sFileName))
return String.Format("~/images/categories/{0}", sFileName);
else if(File.Exists(String.Format("~/images/products/{0}",
sFileName))
return String.Format("~/images/products/{0}", sFileName);
else
return "";
I thought I might have to use:
Server.MapPath.
Any help is appreciated.
Thanks,
Trint- Hide quoted text -

- Show quoted text -

Can you please give me an example?
Thanks,
Trint
 
In your page, you can just do this:

// Get the url.
string url = String.Format("~/images/categories/{0}", sFileName);

// Check to see if the file exists on the server.
if (File.Exists(Server.MapPath(url)))
{
return url;
}
else
{
// Get the url.
url = String.Format("~/images/products/{0}", sFileName);

// Chefk for the existence.
if (File.Exists(Server.MapPath(url)))
{
// Return the url.
return url;
}

// Return an empty string.
return "";
}


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

trint said:
Trint,

Yes, you will have to use the MapPath method to get a path on the
local
machine and then you can use the Exists method on the File class to check
the existence of it.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




How can I do this with my c# code with my website(because the file is
there, but the code doesn't return it)?:
if(File.Exists(String.Format("~/images/categories/{0}", sFileName))
return String.Format("~/images/categories/{0}", sFileName);
else if(File.Exists(String.Format("~/images/products/{0}",
sFileName))
return String.Format("~/images/products/{0}", sFileName);
else
return "";
I thought I might have to use:
Server.MapPath.
Any help is appreciated.
Thanks,
Trint- Hide quoted text -

- Show quoted text -

Can you please give me an example?
Thanks,
Trint
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top