If File.Exists Else question

J

Joseph Oget

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
 
C

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?
 
J

Joseph Oget

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
 
P

Patrice

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...
 
C

CT

You're not accidentally omitting the file name and just using the path? That
would case the problem you're seeing.
 
J

Joseph Oget

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
 
J

Joseph Oget

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
 
J

Jon Skeet [C# MVP]

Joseph Oget said:
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.
 
G

Guest

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?
 
J

Joseph Oget

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.Assembly.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.
 
J

Jon Skeet [C# MVP]

Joseph Oget said:
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.
 
C

Chris_B

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.Assembly.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.vbs) 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 said:
don't understand.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top