364,033 Members | 4865 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

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

trint
P: n/a
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

Apr 23 '07 #1
Share this Question
Share on Google+
3 Replies


Nicholas Paldino [.NET/C# MVP]
P: n/a
Nicholas Paldino [.NET/C# MVP]
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]
- mvp@spam.guard.caspershouse.com

"trint" <trinity.smith@gmail.comwrote in message
news:1177342181.496860.18340@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

trint
P: n/a
trint
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:1177342181.496860.18340@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

Nicholas Paldino [.NET/C# MVP]
P: n/a
Nicholas Paldino [.NET/C# MVP]
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]
- mvp@spam.guard.caspershouse.com

"trint" <trinity.smith@gmail.comwrote in message
news:1177343366.273619.269310@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:1177342181.496860.18340@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

Post your reply

Help answer this question



Didn't find the answer to your C# / C Sharp question?

You can also browse similar questions: C# / C Sharp