How to read a text file in Visual Studio 2019?

-1

Edit: After someone pointed out I was trying to double my directory searching (is that the term to be used?), I corrected it and did a little bit of searching. For anyone else who may find this, here's my current code, which has given me the results I need:

Private Sub ExportCompiledModFileToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExportCompiledModFileToolStripMenuItem.Click
        Dim folderPath As String = CurrentProjectPath
        Dim BlockJSON As System.IO.StreamWriter
        BlockJSON = My.Computer.FileSystem.OpenTextFileWriter(folderPath + "\Export\Data\Objects\Database\ShapeSets\modded_blocks.json", True)
        BlockJSON.Close()
        lstFiles.Items.Clear()
        For Each i In My.Computer.FileSystem.GetFiles(CurrentProjectPath + "\ProjectData\blocks")
            lstFiles.Items.Add(i)
            Dim CurrentFile As String
            CurrentFile = My.Computer.FileSystem.ReadAllText(i)
            BlockJSON = My.Computer.FileSystem.OpenTextFileWriter(folderPath + "\Export\Data\Objects\Database\ShapeSets\modded_blocks.json", True)
            BlockJSON.WriteLine(CurrentFile)
            BlockJSON.Close()
            MsgBox(CurrentFile)

        Next
    End Sub

So I'm trying to read a text file line by line in Visual Studio for a tool, but no matter what I do, I can't get the text from inside the file. Here's the code I'm currently using. Yes, it has a custom extension, but it's a simple text file, which CAN be written to without any extension renaming.

Private Sub ExportCompiledModFileToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExportCompiledModFileToolStripMenuItem.Click
        Dim folderPath As String = CurrentProjectPath
        Dim BlockJSON As System.IO.StreamWriter
        BlockJSON = My.Computer.FileSystem.OpenTextFileWriter(folderPath + "\Export\Data\Objects\Database\ShapeSets\modded_blocks.json", True)
        BlockJSON.Close()
        lstFiles.Items.Clear()
        For Each i In My.Computer.FileSystem.GetFiles(CurrentProjectPath + "\ProjectData\blocks")
            lstFiles.Items.Add(i)
            Dim line As String
            Dim CurrentFile As System.IO.StreamReader
            CurrentFile = My.Computer.FileSystem.OpenTextFileReader(CurrentProjectPath + "\ProjectData\blocks" + i + ".smblock")
            BlockJSON = My.Computer.FileSystem.OpenTextFileWriter(folderPath + "\Export\Data\Objects\Database\ShapeSets\modded_blocks.json", True)
            CurrentFile.ReadToEnd()
            BlockJSON.WriteLine(CurrentFile)
            BlockJSON.Close()
            CurrentFile.Close()

        Next
    End Sub

But every time I press my Export button, I get this error:

System.NotSupportedException
  HResult=0x80131515
  Message=The given path's format is not supported.
  Source=mscorlib
  StackTrace:
   at System.Security.Permissions.FileIOPermission.EmulateFileIOPermissionChecks(String fullPath)
   at System.Security.Permissions.FileIOPermission.QuickDemand(FileIOPermissionAccess access, String fullPath, Boolean checkForDuplicates, Boolean needFullPath)
   at Microsoft.VisualBasic.FileIO.FileSystem.NormalizePath(String Path)
   at Microsoft.VisualBasic.FileIO.FileSystem.OpenTextFileReader(String file, Encoding encoding)
   at Microsoft.VisualBasic.MyServices.FileSystemProxy.OpenTextFileReader(String file)
   at Scrap_Mechanic_Mod_Creator.frmManualInstall.ExportCompiledModFileToolStripMenuItem_Click(Object sender, EventArgs e) in C:\Users\dagam\source\repos\Scrap Mechanic Mod Creator\Form2.vb:line 20
   at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
   at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
   at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
   at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
   at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
   at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ToolStrip.WndProc(Message& m)
   at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   at Scrap_Mechanic_Mod_Creator.My.MyApplication.Main(String[] Args) in :line 81

  This exception was originally thrown at this call stack:
    System.Security.Permissions.FileIOPermission.EmulateFileIOPermissionChecks(string)
    System.Security.Permissions.FileIOPermission.QuickDemand(System.Security.Permissions.FileIOPermissionAccess, string, bool, bool)
    Microsoft.VisualBasic.FileIO.FileSystem.NormalizePath(string)
    Microsoft.VisualBasic.FileIO.FileSystem.OpenTextFileReader(string, System.Text.Encoding)
    Microsoft.VisualBasic.MyServices.FileSystemProxy.OpenTextFileReader(string)
    Scrap_Mechanic_Mod_Creator.frmManualInstall.ExportCompiledModFileToolStripMenuItem_Click(Object, System.EventArgs) in Form2.vb
    System.Windows.Forms.ToolStripItem.RaiseEvent(object, System.EventArgs)
    System.Windows.Forms.ToolStripMenuItem.OnClick(System.EventArgs)
    System.Windows.Forms.ToolStripItem.HandleClick(System.EventArgs)
    System.Windows.Forms.ToolStripItem.HandleMouseUp(System.Windows.Forms.MouseEventArgs)
    ...
    [Call Stack Truncated]

I've tried many ways from multiple videos and sites, including this one. If any one can help, that would be great! ~Erin Rose

vb.net
asked on Stack Overflow Jul 17, 2020 by Erin Rose Webs • edited Jul 18, 2020 by Erin Rose Webs

1 Answer

0

It looks like you want to append the lines from all the files to your BlockJSON file?

See if something like this will simplify life:

Dim path As String = System.IO.Path.Combine(CurrentProjectPath, "ProjectData\blocks")
Dim JSON_path As String = System.IO.Path.Combine(folderPath, "Export\Data\Objects\Database\ShapeSets\modded_blocks.json")
Using BlockJSON As New StreamWriter(JSON_path, False)
    BlockJSON.BaseStream.SetLength(0) ' make sure everything is cleared out and gets overwritten
    For Each fi As FileInfo In New DirectoryInfo(path).GetFiles
        lstFiles.Items.Add(fi.Name)
        For Each line As String In System.IO.File.ReadAllLines(fi.FullName)
            BlockJSON.WriteLine(line)
        Next
    Next
End Using
answered on Stack Overflow Jul 17, 2020 by Idle_Mind • edited Jul 17, 2020 by Idle_Mind

User contributions licensed under CC BY-SA 3.0