首页 > 解决方案 > 如何在 Visual Studio 2019 中读取文本文件?

问题描述

编辑:在有人指出我试图将我的目录搜索翻倍(这是要使用的术语吗?)之后,我更正了它并进行了一些搜索。对于可能找到此内容的其他任何人,这是我当前的代码,它为我提供了所需的结果:

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

因此,我尝试在 Visual Studio 中逐行读取文本文件以获取工具,但无论我做什么,我都无法从文件中获取文本。这是我目前正在使用的代码。是的,它有一个自定义扩展名,但它是一个简单的文本文件,无需任何扩展名重命名即可写入。

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

但是每次我按下导出按钮时,我都会收到此错误:

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]

我从多个视频和网站尝试了很多方法,包括这个。如果有人可以提供帮助,那就太好了!~艾琳·罗斯

标签: vb.net

解决方案


看起来您想将所有文件中的行附加到您的 BlockJSON 文件中?

看看这样的事情是否会简化生活:

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

推荐阅读