469,945 Members | 1,809 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,945 developers. It's quick & easy.

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

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

Apr 23 '07 #1
3 3986
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]
- mv*@spam.guard.caspershouse.com

"trint" <tr***********@gmail.comwrote in message
news:11*********************@o5g2000hsb.googlegrou ps.com...
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

Apr 23 '07 #2
On Apr 23, 11:47 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
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]
- m...@spam.guard.caspershouse.com

"trint" <trinity.sm...@gmail.comwrote in message

news:11*********************@o5g2000hsb.googlegrou ps.com...
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

Apr 23 '07 #3
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]
- mv*@spam.guard.caspershouse.com

"trint" <tr***********@gmail.comwrote in message
news:11**********************@b75g2000hsg.googlegr oups.com...
On Apr 23, 11:47 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
>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]
- m...@spam.guard.caspershouse.com

"trint" <trinity.sm...@gmail.comwrote in message

news:11*********************@o5g2000hsb.googlegro ups.com...
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

Apr 23 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Dante | last post: by
2 posts views Thread by Delali Dzirasa | last post: by
2 posts views Thread by Chuck Anderson | last post: by
2 posts views Thread by yawnmoth | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.