Talk:Visual Basic/Files
From Wikibooks, the open-content textbooks collection
[edit] How to specify the path to a file
I need to know how to use open statement in VB to open a file with variable name. I could do it using:
Open CStr(variable) For Output As #1
but I can not specify the path ...........it shows error and I can not specify the extension .txt ?
please help me
thanks
- The variable simply needs to hold a path to the file. Usually variable would be a string so there is no need to use CStr. The path can be an absolute path such as:
variable = "d:\users\me\my file.txt"
or a file in the current directory (use CurDir to find out what that is):
variable = "my file.txt"
or a path relative to the current directory like this:
variable = "..\you\your file.txt"
or like this:
variable = "mysub\myother file.txt"
For the more adventurous it can also include a reference to a shared file on a server that doesn't use a drive letter:
variable = "\\someserver\someshare\somefile.txt"
I have added this to the page as well. --kwhitefoot 13:37, 2 January 2007 (UTC)