473,398 Members | 2,212 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,398 software developers and data experts.

If File.Exists Else question

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

Similar topics

10
by: alex | last post by:
Hi, I am a beginner with python and here is my first question: How can I read the contents of a file using a loop or something? I open the file with file=open(filename, 'r') and what to do then?...
3
by: StGo | last post by:
How can i read/write file's custom attributs(like subject,author...) in C#??? Thanks :))
2
by: Chris Fink | last post by:
I am using the System.IO.File class to determine if a file exists on a network share. The File.Exists method keeps returning false, even though the file does exist. The MSDN documentation...
2
by: B-Dog | last post by:
Is there a way to make vb.net to overwrite the file when moving? Here is what I'm trying to do: If System.IO.File.Exists(dest) Then 'handle overwrite here If MessageBox.Show("Do you want...
13
by: Joseph Oget | last post by:
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(...
1
by: Tim Failes | last post by:
This seems a trival question, but I cannot get it to work properly... Essentially my question is, how can I create a text file, and guarantee it is given the current date/time as the Creation Time?...
17
by: Peter Duniho | last post by:
I searched using Google, on the web and in the newsgroups, and found nothing on this topic. Hopefully that means I just don't understand what I'm supposed to be doing here. :) The problem: ...
3
by: tshad | last post by:
I have a function that downloads a file to the users computer and it works fine. The problem is that I then want the program to rename the file (file.move) to the same name plus todays date. ...
8
by: Sweetiecakes | last post by:
In our series of "probably something simple"... I am doing a File.Exists operation to check if a file exists. Let's assume that this file is C:/test.txt. Now, if I do...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.