473,396 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

test for existence of file

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
12 1784
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
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
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
....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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Matt | last post by:
Hello, I see other references in this newsgroup saying that the only standard C++ way to test for file existence is some variant of my code below; can someone please confirm...or offer...
13
by: DC Gringo | last post by:
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
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.