|
 |
aspx thread: Using File.IO to check if a file exists
Message #1 by info@e... on Mon, 15 Apr 2002 17:29:58
|
|
Does anyone know how to check if a file exists on a remote server? I
need to check if a picture is available before displaying it. The server
where the pictures are stored is separate from the website server.
Below is what I taught would work and I tried modifying it a few
different ways and it does not give me accurate results. It does
compile and it does run but it does not seem to work. It gives me one
of the two messages below, depending on how I phrase it but it does
not always tell me the right answer. I would appreciate any
assistance. Thanks!
public void showThumb(int prodID)
{
string thumbURL = "http://mysite.com/picture.jpg"
//Check if thumbnail is available on web site
if (!System.IO.File.Exists(thumbURL))
{
//File exists, display image
Response.Write("Image exists");
Response.Write("<img src='" + thumbURL + "'>");
}
else
{
//File does not exist, display error message
Response.Write("Image does not exists");
}
}
Best Regards,
Elmer M.
Message #2 by "Goh Mingkun" <mangokun@h...> on Tue, 16 Apr 2002 09:48:49
|
|
To check whether if a file exists on a remote server, I have a function
here that will return True if that file exists, and False if it doesn't.
Isn't this too good to be true?
__________________________________________________
' Depending on the Web Server which is hosting your file,
' the url to your file may/maynot be case-sensitive
' e.g. the following url will return True
' http://sg.geocities.com/mangokun/images/settings.ico
' HttP://sG.geOcities.com/mAngokun/images/settings.ico
' but the rest below will return False
' http://sg.geocities.com/mangokun/imageS/settings.ico
' http://sg.geocities.com/mangokun/images/Settings.ico
' therefore in my case, sub-dirs and files must follow what I named
them in my web dir.
Public Function IfWReqFileExist(ByVal strUri As String) As Boolean
Try
Dim WReq As WebRequest = WebRequest.Create(strUri)
Dim WResp As WebResponse = WReq.GetResponse()
WResp.Close()
Return True
Catch
Return False
End Try
End Function
' If you like, you can use this instead of the one above
Public Function IfHttpWReqFileExist(ByVal strHttpUri As String) As
Boolean
Try
Dim HttpWReq As HttpWebRequest = CType(WebRequest.Create
(strHttpUri), HttpWebRequest)
' Turn off connection keep-alives.
HttpWReq.KeepAlive = False
Dim HttpWResp As HttpWebResponse = CType(HttpWReq.GetResponse
(), HttpWebResponse)
HttpWResp.Close()
Return True
Catch
Return False
End Try
End Function
__________________________________________________
To test my function, paste the following into HTML View of a WebForm1.aspx.
<asp:TextBox id="TextBox1" style="Z-INDEX: 101;
LEFT: 16px; POSITION: absolute; TOP: 16px" runat="server"
Width="384px">http://sg.geocities.com/mangokun/images/settings.ico</asp:Tex
tBox>
<asp:Button id="btnCheck" style="Z-INDEX: 102;
LEFT: 408px; POSITION: absolute; TOP: 16px" runat="server"
Text="Check"></asp:Button>
<asp:Label id="lblResult" style="Z-INDEX: 103;
LEFT: 16px; POSITION: absolute; TOP: 56px" runat="server"
EnableViewState="False">Result</asp:Label>
And the code-behind for btnCheck,
Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCheck.Click
lblResult.Text = IfWReqFileExist(TextBox1.Text.Trim)
End Sub
After you have press btnCheck at least once,
then try changing the Url to any file that doesn't exist.
The result will then be False.
Message #3 by "Goh Mingkun" <mangokun@h...> on Tue, 16 Apr 2002 10:01:47
|
|
And you must remember to add the System.Net namespace to the top of your
code-behind.
VB
Imports System.Net
C#
using System.Net;
Here is my IfWReqFileExist function in C#:
public bool IfWReqFileExist(string strUri)
{
try
{
WebRequest WReq = WebRequest.Create
(strUri);
WebResponse WResp = WReq.GetResponse();
WResp.Close();
return true;
}
catch
{
return false;
}
}
Message #4 by info@e... on Wed, 17 Apr 2002 02:54:53
|
|
Hello Goh,
Thanks a lot! I tried the C# version and it ran perfectly the first time!
It works great. I didn't bother with the VB version since you offered a C#
but I am sure it works just fine. That was a big help, I was going crazy
troubleshooting the other function. You're the best!
Best Regards,
Elmer M.
> And you must remember to add the System.Net namespace to the top of your
c> ode-behind.
> VB
I> mports System.Net
C> #
u> sing System.Net;
> Here is my IfWReqFileExist function in C#:
> public bool IfWReqFileExist(string strUri)
> {
> try
> {
> WebRequest WReq = WebRequest.Create
(> strUri);
> WebResponse WResp = WReq.GetResponse();
> WResp.Close();
> return true;
> }
> catch
> {
> return false;
> }
> }
|
|
 |