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

If File.Exists Else question

P: n/a
If I use the code below in a VB.NET form, even if the file exists, and is
then executed, the Else statement produces the "Sorry cannot find the File "
error message.

The MessageBox.Show( StrPathToScript ) never gets called! why?

If File.Exists( StrPathToScript ) Then

MessageBox.Show( StrPathToScript )

Else

MessageBox.Show( "Sorry cannot find the File " )

End If

Thanks in advance

Joseph
Oct 14 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
CT
Joseph,

From the MSDN docs: "If the caller does not have sufficient permissions to
read the specified file, no exception is thrown and the method returns false
regardless of the existence of path"

Could that be your problem?

--
Carsten Thomsen
Communities - http://community.integratedsolutions.dk

"Joseph Oget" <ph**********@hotmail.com> wrote in message
news:eK**************@TK2MSFTNGP14.phx.gbl...
If I use the code below in a VB.NET form, even if the file exists, and is
then executed, the Else statement produces the "Sorry cannot find the File
" error message.

The MessageBox.Show( StrPathToScript ) never gets called! why?

If File.Exists( StrPathToScript ) Then

MessageBox.Show( StrPathToScript )

Else

MessageBox.Show( "Sorry cannot find the File " )

End If

Thanks in advance

Joseph

Oct 14 '05 #2

P: n/a
Hi CT,

Thanks for your reply.

The file I attempt to check the existence of, is local to my PC, and in a
folder below where the assembly is, so I do attempt to open it in any way,
only to check if it exist or not.

Joseph
"CT" <ca******@spammersgoawayintegrasol.dk> wrote in message
news:O2*************@TK2MSFTNGP09.phx.gbl...
Joseph,

From the MSDN docs: "If the caller does not have sufficient permissions to
read the specified file, no exception is thrown and the method returns
false regardless of the existence of path"

Could that be your problem?

--
Carsten Thomsen
Communities - http://community.integratedsolutions.dk

"Joseph Oget" <ph**********@hotmail.com> wrote in message
news:eK**************@TK2MSFTNGP14.phx.gbl...
If I use the code below in a VB.NET form, even if the file exists, and
is then executed, the Else statement produces the "Sorry cannot find the
File " error message.

The MessageBox.Show( StrPathToScript ) never gets called! why?

If File.Exists( StrPathToScript ) Then

MessageBox.Show( StrPathToScript )

Else

MessageBox.Show( "Sorry cannot find the File " )

End If

Thanks in advance

Joseph


Oct 14 '05 #3

P: n/a
Could also not have sufficient rights to even now if it exists or now. How
do you handle exceptions ? Else I would double check the path and would try
a location where I'm sure there is no problems...

--
Patrice

"Joseph Oget" <ph**********@hotmail.com> a écrit dans le message de
news:O9****************@TK2MSFTNGP15.phx.gbl...
Hi CT,

Thanks for your reply.

The file I attempt to check the existence of, is local to my PC, and in a
folder below where the assembly is, so I do attempt to open it in any way,
only to check if it exist or not.

Joseph
"CT" <ca******@spammersgoawayintegrasol.dk> wrote in message
news:O2*************@TK2MSFTNGP09.phx.gbl...
Joseph,

From the MSDN docs: "If the caller does not have sufficient permissions to read the specified file, no exception is thrown and the method returns
false regardless of the existence of path"

Could that be your problem?

--
Carsten Thomsen
Communities - http://community.integratedsolutions.dk

"Joseph Oget" <ph**********@hotmail.com> wrote in message
news:eK**************@TK2MSFTNGP14.phx.gbl...
If I use the code below in a VB.NET form, even if the file exists, and
is then executed, the Else statement produces the "Sorry cannot find the File " error message.

The MessageBox.Show( StrPathToScript ) never gets called! why?

If File.Exists( StrPathToScript ) Then

MessageBox.Show( StrPathToScript )

Else

MessageBox.Show( "Sorry cannot find the File " )

End If

Thanks in advance

Joseph



Oct 14 '05 #4

P: n/a
CT
You're not accidentally omitting the file name and just using the path? That
would case the problem you're seeing.

--
Carsten Thomsen
Communities - http://community.integratedsolutions.dk

"Joseph Oget" <ph**********@hotmail.com> wrote in message
news:O9****************@TK2MSFTNGP15.phx.gbl...
Hi CT,

Thanks for your reply.

The file I attempt to check the existence of, is local to my PC, and in a
folder below where the assembly is, so I do attempt to open it in any way,
only to check if it exist or not.

Joseph
"CT" <ca******@spammersgoawayintegrasol.dk> wrote in message
news:O2*************@TK2MSFTNGP09.phx.gbl...
Joseph,

From the MSDN docs: "If the caller does not have sufficient permissions
to read the specified file, no exception is thrown and the method returns
false regardless of the existence of path"

Could that be your problem?

--
Carsten Thomsen
Communities - http://community.integratedsolutions.dk

"Joseph Oget" <ph**********@hotmail.com> wrote in message
news:eK**************@TK2MSFTNGP14.phx.gbl...
If I use the code below in a VB.NET form, even if the file exists, and
is then executed, the Else statement produces the "Sorry cannot find the
File " error message.

The MessageBox.Show( StrPathToScript ) never gets called! why?

If File.Exists( StrPathToScript ) Then

MessageBox.Show( StrPathToScript )

Else

MessageBox.Show( "Sorry cannot find the File " )

End If

Thanks in advance

Joseph



Oct 14 '05 #5

P: n/a
The file exists and is fired up as I expect it to be, so that's not the
problem.
I just that I do not understand why the MessageBox.Show( StrPathToScript )
is not shown before the file is executed.

Joseph
"Patrice" <no****@nowhere.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Could also not have sufficient rights to even now if it exists or now. How
do you handle exceptions ? Else I would double check the path and would
try
a location where I'm sure there is no problems...

--
Patrice

"Joseph Oget" <ph**********@hotmail.com> a écrit dans le message de
news:O9****************@TK2MSFTNGP15.phx.gbl...
Hi CT,

Thanks for your reply.

The file I attempt to check the existence of, is local to my PC, and in a
folder below where the assembly is, so I do attempt to open it in any
way,
only to check if it exist or not.

Joseph
"CT" <ca******@spammersgoawayintegrasol.dk> wrote in message
news:O2*************@TK2MSFTNGP09.phx.gbl...
> Joseph,
>
> From the MSDN docs: "If the caller does not have sufficient permissions to > read the specified file, no exception is thrown and the method returns
> false regardless of the existence of path"
>
> Could that be your problem?
>
> --
> Carsten Thomsen
> Communities - http://community.integratedsolutions.dk
>
> "Joseph Oget" <ph**********@hotmail.com> wrote in message
> news:eK**************@TK2MSFTNGP14.phx.gbl...
>> If I use the code below in a VB.NET form, even if the file exists,
>> and
>> is then executed, the Else statement produces the "Sorry cannot find the >> File " error message.
>>
>> The MessageBox.Show( StrPathToScript ) never gets called! why?
>>
>> If File.Exists( StrPathToScript ) Then
>>
>> MessageBox.Show( StrPathToScript )
>>
>> Else
>>
>> MessageBox.Show( "Sorry cannot find the File " )
>>
>> End If
>>
>> Thanks in advance
>>
>> Joseph
>>
>
>



Oct 14 '05 #6

P: n/a
No, as I said to Patrice the file exists and is found and executed, so why
is the Else statement executed and the If (exist..) isn't?

Joseph
"CT" <ca******@spammersgoawayintegrasol.dk> wrote in message
news:eB**************@TK2MSFTNGP12.phx.gbl...
You're not accidentally omitting the file name and just using the path?
That would case the problem you're seeing.

--
Carsten Thomsen
Communities - http://community.integratedsolutions.dk

"Joseph Oget" <ph**********@hotmail.com> wrote in message
news:O9****************@TK2MSFTNGP15.phx.gbl...
Hi CT,

Thanks for your reply.

The file I attempt to check the existence of, is local to my PC, and in a
folder below where the assembly is, so I do attempt to open it in any
way, only to check if it exist or not.

Joseph
"CT" <ca******@spammersgoawayintegrasol.dk> wrote in message
news:O2*************@TK2MSFTNGP09.phx.gbl...
Joseph,

From the MSDN docs: "If the caller does not have sufficient permissions
to read the specified file, no exception is thrown and the method
returns false regardless of the existence of path"

Could that be your problem?

--
Carsten Thomsen
Communities - http://community.integratedsolutions.dk

"Joseph Oget" <ph**********@hotmail.com> wrote in message
news:eK**************@TK2MSFTNGP14.phx.gbl...
If I use the code below in a VB.NET form, even if the file exists, and
is then executed, the Else statement produces the "Sorry cannot find
the File " error message.

The MessageBox.Show( StrPathToScript ) never gets called! why?

If File.Exists( StrPathToScript ) Then

MessageBox.Show( StrPathToScript )

Else

MessageBox.Show( "Sorry cannot find the File " )

End If

Thanks in advance

Joseph



Oct 14 '05 #7

P: n/a
Joseph Oget <ph**********@hotmail.com> wrote:
No, as I said to Patrice the file exists and is found and executed, so why
is the Else statement executed and the If (exist..) isn't?


Well, you haven't shown the code which executes the file, which doesn't
help. If you're just starting a new process, it's quite possible that
that's searching the path for the file, which File.Exists won't.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 14 '05 #8

P: n/a
Ron
What do you mean by "the file is executed"? Where is the code that "executes"
the file? Have you step through the code in debug mode and checked the value
of StrPathToScript?

"Joseph Oget" wrote:
If I use the code below in a VB.NET form, even if the file exists, and is
then executed, the Else statement produces the "Sorry cannot find the File "
error message.

The MessageBox.Show( StrPathToScript ) never gets called! why?

If File.Exists( StrPathToScript ) Then

MessageBox.Show( StrPathToScript )

Else

MessageBox.Show( "Sorry cannot find the File " )

End If

Thanks in advance

Joseph

Oct 14 '05 #9

P: n/a
Hi Jon,

Thanks for your reply. The code I use is below:
Dim strPath As String = AppPath & "\"
Public Function AppPath() As String

Return
System.IO.Path.GetDirectoryName(Reflection.Assembl y.GetEntryAssembly().Location)

End Function
Private Sub JVM_Clicked(sender As object, e As System.EventArgs)
Dim StrPathToScript As String = """" & strPath &
"Scripts_Files.vbs" & """"
Dim strCmd As String = "C:\WINNT\system32\WScript.exe "
If File.Exists( StrPathToScript ) Then
MessageBox.Show( StrPathToScript )
Else
MessageBox.Show( "Can't find the F*****g
file " )
End If
End Sub
The location of that file to be found and executed is relative and below the
location of the assembly:
The strPath returns the path to the Assembly coorectly, and the file to be
executed does exist and is found.

My problem is that there will be cases when I will want to copy the GUI to a
PC which I will remotely PCAnywhere into and may not want to download the
script file(s) - the GUI has a dozen buttons all pointing to various
scripts - so that what I am trying to achieve now, as proven way to either
execute a local file, if it is found, or a remote file if that's what I want
to do.

I hope I made things clearer this time round, TIA

joseph.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Joseph Oget <ph**********@hotmail.com> wrote:
No, as I said to Patrice the file exists and is found and executed, so
why
is the Else statement executed and the If (exist..) isn't?


Well, you haven't shown the code which executes the file, which doesn't
help. If you're just starting a new process, it's quite possible that
that's searching the path for the file, which File.Exists won't.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Oct 17 '05 #10

P: n/a
Joseph Oget <ph**********@hotmail.com> wrote:
Thanks for your reply. The code I use is below:


No, please post a short but *complete* example, so that we can try to
reproduce the problem.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 17 '05 #11

P: n/a
don't understand.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Joseph Oget <ph**********@hotmail.com> wrote:
Thanks for your reply. The code I use is below:


No, please post a short but *complete* example, so that we can try to
reproduce the problem.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Oct 18 '05 #12

P: n/a
Joseph Oget <ph**********@hotmail.com> wrote:
don't understand.


I asked you to post a complete program. One which I could cut and paste
into a text editor, compile, and run. You posted a code snippet.

Again, please read http://www.pobox.com/~skeet/csharp/complete.html to
see what I'm looking for.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 18 '05 #13

P: n/a
The following code (albeit a snippet) works on VB.NET

'First Bit Exactly the same:
Dim strPath As String = AppPath & "\"

Public Function AppPath() As String

Return
System.IO.Path.GetDirectoryName(Reflection.Assembl y.GetEntryAssembly().Location)

End Function

'Then your sub (I've put this into a form1_load, but you can change that)

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

'In the next line I took out the spare double quotes

Dim StrPathToScript As String = strPath & "Scripts_Files.vbs"

Dim strCmd As String = "C:\WINNT\system32\WScript.exe "

'In the next line I have explicitly declared the File.Exists statement

'You could, of course, do an

'Imports system.io

'at the top of your code (which you may have)

If System.IO.File.Exists(StrPathToScript) Then

MessageBox.Show(StrPathToScript)

Else

MessageBox.Show("Can't find the file")

End If

End Sub

'End of code snippet

When a file called scripts_files.vbs is put in the run directory (i.e. My
Projects\WindowsApplication1\Debug\scripts_files.v bs) the app reports the
file as found. If you then delete, rename or move the file, it puts up the
"can't find it" message.

"Joseph Oget" <ph**********@hotmail.com> wrote in message
news:u0**************@TK2MSFTNGP14.phx.gbl...
don't understand.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Joseph Oget <ph**********@hotmail.com> wrote:
Thanks for your reply. The code I use is below:


No, please post a short but *complete* example, so that we can try to
reproduce the problem.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Oct 19 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.