View Single Post
  #6 (permalink)  
Old December 31st, 2011, 05:13 PM
Rod Stephens's Avatar
Rod Stephens Rod Stephens is offline
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

No problem.

First a note.

Code:
stream_writer = System.IO.File.AppendText("c:\WaterWriter\" & (strSaveAs) & ".txt")
Here the parentheses around strSaveAs aren't necessary and confuse things a little bit. What parentheses mean to VB in this situation is, "Evaluate the enclosed thing and use the result." So here it's actually creating a new temporary variable with the same value as strSaveAs and using that. It doesn't make any real difference in this case but there are situations where that can be confusing so I try to avoid unnecessary parentheses.

But to the main question. My suspicion is that C:\WaterWriter\Library\ doesn't already exist. The AppendText method will create the file if it doesn't already exist but I don't think it will create directories if they're not already there.

One way to create the directory is to do something like this:

Code:
My.Computer.FileSystem.CreateDirectory("C:\WaterWriter\Library\")
If the directory already exists, it should quietly return without throwing an error.

I think it can even create intermediary directories. For example, if C:\DirA exists, then it can create C:\DirA\DirB\DirC\DirD all at once.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote