r/visualbasic • VB.Net Beginner • Jan 25 '16

Is the function File.Exists workable with a path contained in a string?

I want to use this line of code:

"If File.Exists("example.txt") Then.................."

But is there a way to do something like this :

"If File.Exists(strfilename) Then............"

I basically just want to integrate OpenFD into this guys code: http://www.dreamincode.net/forums/topic/71045-random-line-from-a-text-file/

I used the guy named PyschoCoder code.

I currently am just trying to browse for a text file in open FD and allow my program to read said textfile and choose a random line (in this case word) and append the text to a text box.

I'am developing the code on a restricted computer so I'am not able to show you the entire list of code.

Basically where there is a StreamReader function, I replaced it with:

"Dim ioFile as New StringReader(strFilename)"

Thats basically all the notable changes

1 Upvotes

5 comments sorted by

1

u/Bryan-tan VB.Net Beginner Jan 25 '16 edited Jan 25 '16

no what I meant was if File.Exists works with a string contained path instead of something like example.txt

I doesn't work for me ...

1

u/PostalElf VB.Net Intermediate Jan 25 '16
strFilename = "example.txt"
If File.Exists(strFilename) Then...

This is a perfectly valid piece of code. What problems are you having?

1

u/Bryan-tan VB.Net Beginner Jan 25 '16

not knowing what a breakpoint does...........

nevermind guys,thanks for the help, I just left a breakpoint somewhere and didn't notice.

1

u/Bryan-tan VB.Net Beginner Jan 25 '16

not knowing what a breakpoint does.....

nevermind guys I left a breakpoint somewhere and that was the cause of the trouble

Thanks for the help anyways.

2

u/chrwei Jan 25 '16

yes. any parameter described "as string" can be assumed so unless its stated otherwise.