The problem I am having is that when I use FileSystem.CreateDirectory it creates a directory but not a file in that directory in which to accept data. As commented, if my variable filePath is input with something like: C:\newfile.txt, the data is written to to that file just fine. However, if the file path is input as c:\newfolder\newfile.txt or c:\newfolder\newfile the folders are made, (in the instance of the first example, newfile.txt is created as a folder (instead of a file) un the folder, "newfolder". In either case, the program stops with an exception.
Therefore, it is evident to me that " My.Computer.FileSystem.CreateDirectory(filePath)" only creates folders despite the path including a file in the program.
There is another method (from documentation) using FileStream instead of FileSystem that seems to create a path and a file:
But I need help with meshing the two systems (if there is not a seperate way creating a file using a single varible combining the path and file name, using Filesystem), while not having to give up my For-Next loop to write the data to the file. Any suggestions for some elegant programming that does not require I abandon my existing For-Next loop?
Therefore, it is evident to me that " My.Computer.FileSystem.CreateDirectory(filePath)" only creates folders despite the path including a file in the program.
vb Code:
'********************************************************* Public Sub ToFile(ByVal StrPadding As Integer()) Dim i As Integer 'Creates dirctory. My.Computer.FileSystem.CreateDirectory(filePath) 'writes text to file (only if file is located in the root). For i = 0 To 9 My.Computer.FileSystem.WriteAllText(filePath, CStr(StrPadding(i)), True) Next End Sub '**********************************************************
vb Code:
Imports System Imports System.IO Imports System.Text Module Module1 Sub Main() Dim path As String = "c:\temp\MyTest.txt" ' Create or overwrite the file. Dim fs As FileStream = File.Create(path) ' Add text to the file. Dim info As Byte() = New UTF8Encoding(True).GetBytes("This is some text in the file.") fs.Write(info, 0, info.Length) fs.Close() End Sub End Module