By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,612 Members | 1,632 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,612 IT Pros & Developers. It's quick & easy.

test for existence of file

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

If FileExists(myVariable & ".pdf") = True
pnlMyPanel.Visible = True
End If

--
_____
DC G
Nov 21 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Use the File.Exists method.

"DC Gringo" <dc******@visiontechnology.net> wrote in message
news:e4**************@TK2MSFTNGP10.phx.gbl...
How do I test for existence of a file in the file system:

If FileExists(myVariable & ".pdf") = True
pnlMyPanel.Visible = True
End If

--
_____
DC G

Nov 21 '05 #2

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

--
mvh
Svein Terje Gaup

"DC Gringo" <dc******@visiontechnology.net> wrote in message
news:e4**************@TK2MSFTNGP10.phx.gbl...
How do I test for existence of a file in the file system:

If FileExists(myVariable & ".pdf") = True
pnlMyPanel.Visible = True
End If

--
_____
DC G

Nov 21 '05 #3

P: n/a
Dim myFile As FileInfo = New FileInfo(myFileName)
If(myFile.Exists)
myPanel.Visible = True
End If

Sincerely
Svein Terje Gaup

"DC Gringo" <dc******@visiontechnology.net> wrote in message
news:e4**************@TK2MSFTNGP10.phx.gbl...
How do I test for existence of a file in the file system:

If FileExists(myVariable & ".pdf") = True
pnlMyPanel.Visible = True
End If

--
_____
DC G

Nov 21 '05 #4

P: n/a
....or just use the File class of the System.IO namespace:
If (System.IO.File.Exists(myFile)) Then
myPanel.Visible = True
End If

--
Hope this helps ...

Rene Mansveld
Spider IT - Germany (was Whitworth Software Solutions)
www.Spider-IT.de / www.Spider-IT.net / www.Spider-IT.org / www.Spider-IT.biz

Please reply to the newsgroup(s) :o)

"Svein Terje Gaup" <st****@broadpark.no.spam> schrieb im Newsbeitrag
news:OG**************@TK2MSFTNGP09.phx.gbl...
Dim myFile As FileInfo = New FileInfo(myFileName)
If(myFile.Exists)
myPanel.Visible = True
End If

Sincerely
Svein Terje Gaup

"DC Gringo" <dc******@visiontechnology.net> wrote in message
news:e4**************@TK2MSFTNGP10.phx.gbl...
How do I test for existence of a file in the file system:

If FileExists(myVariable & ".pdf") = True
pnlMyPanel.Visible = True
End If

--
_____
DC G


Nov 21 '05 #5

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

"Rene Mansveld" <R.********@TAKETHISOUT.Spider-IT.de> wrote in message
news:eF**************@TK2MSFTNGP10.phx.gbl...
...or just use the File class of the System.IO namespace:
If (System.IO.File.Exists(myFile)) Then
myPanel.Visible = True
End If

--
Hope this helps ...

Rene Mansveld
Spider IT - Germany (was Whitworth Software Solutions)
www.Spider-IT.de / www.Spider-IT.net / www.Spider-IT.org / www.Spider-IT.biz
Please reply to the newsgroup(s) :o)

"Svein Terje Gaup" <st****@broadpark.no.spam> schrieb im Newsbeitrag
news:OG**************@TK2MSFTNGP09.phx.gbl...
Dim myFile As FileInfo = New FileInfo(myFileName)
If(myFile.Exists)
myPanel.Visible = True
End If

Sincerely
Svein Terje Gaup

"DC Gringo" <dc******@visiontechnology.net> wrote in message
news:e4**************@TK2MSFTNGP10.phx.gbl...
How do I test for existence of a file in the file system:

If FileExists(myVariable & ".pdf") = True
pnlMyPanel.Visible = True
End If

--
_____
DC G



Nov 21 '05 #6

P: n/a
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".
"DC Gringo" <dc******@visiontechnology.net> wrote in message
news:Og**************@TK2MSFTNGP09.phx.gbl...
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

"Rene Mansveld" <R.********@TAKETHISOUT.Spider-IT.de> wrote in message
news:eF**************@TK2MSFTNGP10.phx.gbl...
...or just use the File class of the System.IO namespace:
If (System.IO.File.Exists(myFile)) Then
myPanel.Visible = True
End If

--
Hope this helps ...

Rene Mansveld
Spider IT - Germany (was Whitworth Software Solutions)
www.Spider-IT.de / www.Spider-IT.net / www.Spider-IT.org /

www.Spider-IT.biz

Please reply to the newsgroup(s) :o)

"Svein Terje Gaup" <st****@broadpark.no.spam> schrieb im Newsbeitrag
news:OG**************@TK2MSFTNGP09.phx.gbl...
Dim myFile As FileInfo = New FileInfo(myFileName)
If(myFile.Exists)
myPanel.Visible = True
End If

Sincerely
Svein Terje Gaup

"DC Gringo" <dc******@visiontechnology.net> wrote in message
news:e4**************@TK2MSFTNGP10.phx.gbl...
> How do I test for existence of a file in the file system:
>
> If FileExists(myVariable & ".pdf") = True
> pnlMyPanel.Visible = True
> End If
>
> --
> _____
> DC G
>
>



Nov 21 '05 #7

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

"DC Gringo" <dc******@visiontechnology.net> wrote in message
news:Og**************@TK2MSFTNGP09.phx.gbl...
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

"Rene Mansveld" <R.********@TAKETHISOUT.Spider-IT.de> wrote in message
news:eF**************@TK2MSFTNGP10.phx.gbl...
...or just use the File class of the System.IO namespace:
If (System.IO.File.Exists(myFile)) Then
myPanel.Visible = True
End If

--
Hope this helps ...

Rene Mansveld
Spider IT - Germany (was Whitworth Software Solutions)
www.Spider-IT.de / www.Spider-IT.net / www.Spider-IT.org /

www.Spider-IT.biz

Please reply to the newsgroup(s) :o)

"Svein Terje Gaup" <st****@broadpark.no.spam> schrieb im Newsbeitrag
news:OG**************@TK2MSFTNGP09.phx.gbl...
Dim myFile As FileInfo = New FileInfo(myFileName)
If(myFile.Exists)
myPanel.Visible = True
End If

Sincerely
Svein Terje Gaup

"DC Gringo" <dc******@visiontechnology.net> wrote in message
news:e4**************@TK2MSFTNGP10.phx.gbl...
> How do I test for existence of a file in the file system:
>
> If FileExists(myVariable & ".pdf") = True
> pnlMyPanel.Visible = True
> End If
>
> --
> _____
> DC G
>
>



Nov 21 '05 #8

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

"Rene Mansveld" <R.********@TAKETHISOUT.Spider-IT.de> wrote in message
news:eF**************@TK2MSFTNGP10.phx.gbl...
...or just use the File class of the System.IO namespace:
If (System.IO.File.Exists(myFile)) Then
myPanel.Visible = True
End If

--
Hope this helps ...

Rene Mansveld
Spider IT - Germany (was Whitworth Software Solutions)
www.Spider-IT.de / www.Spider-IT.net / www.Spider-IT.org /

www.Spider-IT.biz

Please reply to the newsgroup(s) :o)

"Svein Terje Gaup" <st****@broadpark.no.spam> schrieb im Newsbeitrag
news:OG**************@TK2MSFTNGP09.phx.gbl...
Dim myFile As FileInfo = New FileInfo(myFileName)
If(myFile.Exists)
myPanel.Visible = True
End If

Sincerely
Svein Terje Gaup

"DC Gringo" <dc******@visiontechnology.net> wrote in message
news:e4**************@TK2MSFTNGP10.phx.gbl...
> How do I test for existence of a file in the file system:
>
> If FileExists(myVariable & ".pdf") = True
> pnlMyPanel.Visible = True
> End If
>
> --
> _____
> DC G
>
>



Nov 21 '05 #9

P: n/a
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
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:O7**************@TK2MSFTNGP15.phx.gbl...
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"
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

"Rene Mansveld" <R.********@TAKETHISOUT.Spider-IT.de> wrote in message
news:eF**************@TK2MSFTNGP10.phx.gbl...
...or just use the File class of the System.IO namespace:
If (System.IO.File.Exists(myFile)) Then
myPanel.Visible = True
End If

--
Hope this helps ...

Rene Mansveld
Spider IT - Germany (was Whitworth Software Solutions)
www.Spider-IT.de / www.Spider-IT.net / www.Spider-IT.org /

www.Spider-IT.biz

Please reply to the newsgroup(s) :o)

"Svein Terje Gaup" <st****@broadpark.no.spam> schrieb im Newsbeitrag
news:OG**************@TK2MSFTNGP09.phx.gbl...
> Dim myFile As FileInfo = New FileInfo(myFileName)
> If(myFile.Exists)
> myPanel.Visible = True
> End If
>
> Sincerely
> Svein Terje Gaup
>
> "DC Gringo" <dc******@visiontechnology.net> wrote in message
> news:e4**************@TK2MSFTNGP10.phx.gbl...
> > How do I test for existence of a file in the file system:
> >
> > If FileExists(myVariable & ".pdf") = True
> > pnlMyPanel.Visible = True
> > End If
> >
> > --
> > _____
> > DC G
> >
> >
>
>



Nov 21 '05 #10

P: n/a
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 Gringo" <dc******@visiontechnology.net> wrote in message
news:OG**************@TK2MSFTNGP09.phx.gbl...
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
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:O7**************@TK2MSFTNGP15.phx.gbl...
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"
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

"Rene Mansveld" <R.********@TAKETHISOUT.Spider-IT.de> wrote in message
news:eF**************@TK2MSFTNGP10.phx.gbl...
> ...or just use the File class of the System.IO namespace:
> If (System.IO.File.Exists(myFile)) Then
> myPanel.Visible = True
> End If
>
> --
> Hope this helps ...
>
> Rene Mansveld
> Spider IT - Germany (was Whitworth Software Solutions)
> www.Spider-IT.de / www.Spider-IT.net / www.Spider-IT.org /
www.Spider-IT.biz
>
> Please reply to the newsgroup(s) :o)
>
> "Svein Terje Gaup" <st****@broadpark.no.spam> schrieb im Newsbeitrag
> news:OG**************@TK2MSFTNGP09.phx.gbl...
> > Dim myFile As FileInfo = New FileInfo(myFileName)
> > If(myFile.Exists)
> > myPanel.Visible = True
> > End If
> >
> > Sincerely
> > Svein Terje Gaup
> >
> > "DC Gringo" <dc******@visiontechnology.net> wrote in message
> > news:e4**************@TK2MSFTNGP10.phx.gbl...
> > > How do I test for existence of a file in the file system:
> > >
> > > If FileExists(myVariable & ".pdf") = True
> > > pnlMyPanel.Visible = True
> > > End If
> > >
> > > --
> > > _____
> > > DC G
> > >
> > >
> >
> >
>
>



Nov 21 '05 #11

P: n/a
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
"DC Gringo" <dc******@visiontechnology.net>
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...

Nov 21 '05 #12

P: n/a
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
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:u6**************@tk2msftngp13.phx.gbl...
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
"DC Gringo" <dc******@visiontechnology.net>
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...


Nov 21 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.