test for existence of file

  • Thread starter Thread starter DC Gringo
  • Start date Start date
D

DC Gringo

How do I test for existence of a file in the file system:

If FileExists(myVariable & ".pdf") = True
pnlMyPanel.Visible = True
End If
 
You should use a System.IO.FileInfo object.

The C# code for doing what you want is like this:

FileInfo myFile = new FileInfo(myVariable + ".pdf")
if(myFile.Exists)
{
pnlMyPanel.Visible = true;
}
 
Dim myFile As FileInfo = New FileInfo(myFileName)
If(myFile.Exists)
myPanel.Visible = True
End If

Sincerely
Svein Terje Gaup
 
Ok, I'm trying this but is not working. I'm using a path to and from a
webserver location...should that make a difference?

If (System.IO.File.Exists("../folder/anotherfolder/" & aVariable&
"/adocument.pdf")) Then

pnlIntro.Visible = True

End If



_____

DC G
 
I think you are going to need an absolute path there.. even if you derive it
from your current assembly - I don't think it assumes a "current working
directory".
 
Don't ever mix up file IO and web server. They are not, never have been, and
never will be, the same. Well, maybe "never will be" is a bit overconfident.
System.IO is a namespace for working with file system objects, NOT web
server objects. That means that the file system will never (again, I may be
a bit too bold to say "never") be able to understand a URL. You are in luck
in one sense, however. The ASP.Net HTTPServerUtility has a method which can
translate a URL into a file location: MapPath(). Use Server.MapPath(url) to
convert the URL to a file location, then check if it exists with the correct
path.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
 
Hello DC,

Since you're in a web application, try this:

string file = Server.MapPath("../folder/anotherfolder/" + aVariable + "/adocument.pdf");
if (File.Exists(file))
{
pnlIntro.Visible=true;
}
 
Still no beans...here's my code...



Dim theFile As String

theFile = Server.MapPath("/livelihoods/files/" & countryCode & "/intro.pdf")

If System.IO.File.Exists(theFile) = True Then

pnlIntro.Visible = True

End If
 
Hello DC,

theFile = Server.MapPath("~/livelihoods/files/" & countryCode & "/intro.pdf")

Notice the ~...

The ~ is an alias for the current virtual root. Otherwise you'd be looking in the web applications root folder (ie: c:\inetpub\wwwroot\livelihoods)
 
Really getting frustrated here. I've got the MapPath working and spewing
out the right path...I am able to print it out on the page with <% =
theFileMapped %>. It's just not catching on my conditional statement in the
code behind and making my panel visible. I can turn the panel on and off
manually in the <asp:label visible=> attribute, but it just doesn't work in
my code behind.

In my page:
--------------------
<ASP:PANEL ID="pnlIntro" VISIBLE="False" RUNAT="server"><SPAN
STYLE="FONT-SIZE: 10px">. </SPAN>
<A CLASS="secondNav"
HREF="/livelihoods/<% = countryCode %>/intro.pdf"
TARGET="_blank">Zoning/Profiling Intro</A><BR>
</ASP:PANEL>


In my code-behind
----------------------------
Public theFile As String
Public theFileMapped As String

Protected WithEvents pnlIntro As System.Web.UI.WebControls.Panel

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

theFile = "~/livelihoods/files/" & countryCode & "/intro.pdf"
theFileMapped = Server.MapPath(theFile)

If System.IO.File.Exists(theFileMapped) Then
pnlIntro.Visible = True
End If

End Sub
_____
DC G

Matt Berther said:
Hello DC,

theFile = Server.MapPath("~/livelihoods/files/" & countryCode & "/intro.pdf")

Notice the ~...

The ~ is an alias for the current virtual root. Otherwise you'd be looking
in the web applications root folder (ie: c:\inetpub\wwwroot\livelihoods)
 
DC Gringo,

From the start of the thread I got the idea that you want to use code on the
client side, you cannot.

You can use this code on the serverside either with a webapplication or with
a webservice application. However than should the application have proper
rights to the path's you are using.

I hope this helps?

Cor

"DC Gringo"
 
Hmm.. not sure about that.. (I don't mean that in a sarcastic way, I mean I
really don't know if that's possible) - but what I would use is a
LinkButton - which looks like a regular link, then in your code-behind,
dynamically generate the NavigateUrl property..

DC Gringo said:
Really getting frustrated here. I've got the MapPath working and spewing
out the right path...I am able to print it out on the page with <% =
theFileMapped %>. It's just not catching on my conditional statement in the
code behind and making my panel visible. I can turn the panel on and off
manually in the <asp:label visible=> attribute, but it just doesn't work in
my code behind.

In my page:
--------------------
<ASP:PANEL ID="pnlIntro" VISIBLE="False" RUNAT="server"><SPAN
STYLE="FONT-SIZE: 10px">. </SPAN>
<A CLASS="secondNav"
HREF="/livelihoods/<% = countryCode %>/intro.pdf"
TARGET="_blank">Zoning/Profiling Intro</A><BR>
</ASP:PANEL>


In my code-behind
----------------------------
Public theFile As String
Public theFileMapped As String

Protected WithEvents pnlIntro As System.Web.UI.WebControls.Panel

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

theFile = "~/livelihoods/files/" & countryCode & "/intro.pdf"
theFileMapped = Server.MapPath(theFile)

If System.IO.File.Exists(theFileMapped) Then
pnlIntro.Visible = True
End If

End Sub
_____
DC G

Matt Berther said:
Hello DC,

theFile = Server.MapPath("~/livelihoods/files/" & countryCode & "/intro.pdf")

Notice the ~...

The ~ is an alias for the current virtual root. Otherwise you'd be
looking
in the web applications root folder (ie: c:\inetpub\wwwroot\livelihoods)
 
I am using this on the server side...I'm determining the existence of a file
on the server side in the page_load of the code-behind to tell the .net
application server whether or not to include a panel in the .html output
that goes out to the client.

I give the "everyone" user full control and still am unable to read the
existence of the file...

_____
dC G
 
Then what about something like this:

Dim strPath as String
strPath = Server.MapPath(".") ' Get the actual physical path of the current
directory
strPath += "../folder/anotherfolder/" & aVariable & "/somefile.pdf"
Response.Write("Path: [" & strPath & "]") ' Write it out to the screen
If (System.IO.File.Exists(strPath)) Then
' Do something
End If


The key difference is that File.Exists can't take relative paths.. so you
need to explicitly tell it EXACTLY what drive, directory and file you are
looking for..

HTH
 
DC,

This I tried as test and no problem at all.

Cor
\\\
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If IO.File.Exists("C:\mytestimage.jpg") Then
Me.Label1.Text = "It is there"
Else
Me.Label1.Text = "It is not there"
End If

End Sub
///
Cor
 
Well,

I have found that this does work on another machine...it seems to be
permissions problem...oy yayayaay

Thank you all for you help and attention.

_____
DC G
 
Back
Top