-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

  • 1
    `The given path's format is not supported.` You should check what is the path you're trying to open with `OpenTextFileReader()`. By the way you can use `IO.Path.Combine()` to concatenate path parts without bothering with adding/checking for separators. – Amessihel Jul 17 '20 at 19:44
  • @Amessihel The path is correct, as I've written to this path and have copy and pasted this path from previous code. And thanks for the info on `IO.Path.Combine()`, I'll be using that! – Erin Rose Webs Jul 17 '20 at 19:55
  • You''re welcome! What does the path look like? – Amessihel Jul 17 '20 at 20:20
  • @Amessihel the entire path it's trying to access? `C:\Users\**redacted**\OneDrive\Documents\Scrap Mechanic Mod Creator\workspaces\test14\Export\Data\Objects\Database\ShapeSets\modded_blocks.json`. The files that I need to read are `C:\Users\**redacted**\OneDrive\Documents\Scrap Mechanic Mod Creator\workspaces\test14\ProjectData\blocks\a.smblock`. I have the separators cause I have the file detection automated. – Erin Rose Webs Jul 17 '20 at 20:34
  • "`*`" Isn't a valid character for a file/directory name. – Amessihel Jul 17 '20 at 20:38
  • That's not my actual User folder, I just hid it and tried to bold it – Erin Rose Webs Jul 17 '20 at 20:44
  • 1
    According to [another SO question](https://stackoverflow.com/questions/7348768/the-given-paths-format-is-not-supported) it may be related to several causes, an invisible character for example. Try to replace all your concatenation by Path.Combine calls doing them step by step until you caught the culprit. – Amessihel Jul 17 '20 at 20:52
  • You tell us what the path should be but I'd wager that you never bothered to look at what the path actually is that is being used. That's what's important. If it was what you thought it was then there wouldn't be an issue in the first place. Given that `i` is the full path of a file that already contains `CurrentProjectPath`, how does it make sense to do this: `CurrentProjectPath + "\ProjectData\blocks" + i + ".smblock"`? What do you think that is going to produce? Did you bother to look to see what it actually does produce? Fairly obviously not. – jmcilhinney Jul 18 '20 at 03:27
  • That's why you need to actually debug your code. Don't just read it because, when you do that, you will likely see what you expect to see and not what's actually there. If you debug the code with breakpoints, stepping and examining state then will see what's actually there. It's also why you should always use descriptive variable names. If you used `filePath` for that variable instead of `i` then it would be obvious that concatenating it with more partial paths made no sense. The name `i` implies a loop counter and it seems you may have used it that way. – jmcilhinney Jul 18 '20 at 03:42
  • Okay, I really do appreciate it! Have a great day, mate! – Erin Rose Webs Jul 18 '20 at 16:21

1 Answers1

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
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • `JSON_path = My.Computer.FileSystem.OpenTextFileWriter(JSON_path, True)` underlines red, issue being `Value of 'StreamWriter' cannot be converted to 'String'`, and `JSON_path.WriteLine(line)` throws `'WriteLine is not a valid member of 'String'`. I've tried to change JSON_path from String to StreamWriter, but it didn't work. – Erin Rose Webs Jul 17 '20 at 20:47
  • Okay...but if you look at MY CODE, I'm not assigning OpenTextFileWriter() to JSON_path. Why are you changing it? – Idle_Mind Jul 17 '20 at 20:58
  • The BlockJSON wasn't being dimmed in. – Erin Rose Webs Jul 17 '20 at 21:06
  • It was `Dim`med separately in your original code; I just left it there. Updated code above. – Idle_Mind Jul 17 '20 at 21:10
  • `System.IO.DirectoryNotFoundException: 'Could not find a part of the path` I think it;s close, but this Exception Unhandled is thrown at `Dim BlockJSON As System.IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(JSON_path, True)`. I've tried finagling it myself, and nothing. – Erin Rose Webs Jul 17 '20 at 22:10
  • Put a [BREAKPOINT](https://learn.microsoft.com/en-us/visualstudio/debugger/using-breakpoints?view=vs-2019) on that line and see what is in the "JSON_path" variable. Post back with its value. You could also just use `Debug.print(JSON_path)` and look in the IDE for it... – Idle_Mind Jul 17 '20 at 22:12
  • Crap, sorry. I've used this language before, but stepped away for a year, so I forgot a lot of functions and their usage. It equals the proper directory, just for some reason it's not making the file anymore. I'll try making the file when I make the directories, though I wouldn't prefer that. – Erin Rose Webs Jul 17 '20 at 22:24
  • Do you want that file to be EMPTIED before the loop, and then appended with all the lines from the other files? – Idle_Mind Jul 17 '20 at 22:25
  • It's supposed to be generated when I click the export button, so the file is created empty on click, then is filled with the data. So basically yeah. And sorry if I'm annoying you with my stupidity. – Erin Rose Webs Jul 17 '20 at 22:27
  • Unfortunately it throws the same Exception Unhandled on `Using BlockJSON As new StreamWriter(JSON_path, False)` I honestly have no idea why this is happening, as normally the file would be created. idk, I'll see if restarting Visual Studio will help. – Erin Rose Webs Jul 17 '20 at 22:46
  • Well, either you don't have permissions for that path, or the file is LOCKED from another process and this is causing the exception. – Idle_Mind Jul 17 '20 at 22:50
  • Okay. Thanks for helping, I really do appreciate it! – Erin Rose Webs Jul 17 '20 at 23:05