0

我正在尝试编辑我的代码以打开文件夹 ATC 中的所有文件(包括任何子目录)

如果发现这些文件中的任何一个不包含文本“Index...”,则附加该特定文件以包含在文档“C:\stuff.txt”中找到的文本,然后将文件与更改一起保存...</p >

我遇到的问题是文件名。

第一个问题是我的 TARGET_FILE 无法打开;第二个问题是我的 TARGET_FILE 不会保存?

有人可以帮忙吗?我已经用 '<<----ISSUE #1 & '<<----ISSUE #2 标记了代码,也指出了问题所在。

Imports System.IO
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    'Get folder containing files to ellipherate through...
    Dim TARGET_FILE() As String = IO.Directory.GetFiles("C:\VTS\TREADSTONE LT\ATC", "*", SearchOption.AllDirectories)

    For Each file As String In TARGET_FILE

        Dim sReader As New StreamReader(file)
        Dim content As String = sReader.ReadToEnd()
        sReader.Close()

        If Text.Contains("Index...") Then
            'do nothing
        Else
            Dim text As String = IO.File.ReadAllText(TARGET_FILE) '<<----ISSUE #1
            Dim EXTRA As String = IO.File.ReadAllText("C:\STUFF.TXT")
            Dim index As Integer = text.IndexOf("<Tools>")
            Dim countChars As Integer
            countChars = "<Tools>".Length
            If index >= 0 Then

                ' String is in file, starting at character "<Tools>" insert text "TEST_HELLO"
                text = text.Insert(index + countChars, EXTRA)

                Dim Writer As System.IO.StreamWriter
                Writer = New System.IO.StreamWriter(TARGET_FILE) '<<----ISSUE #2
                Writer.Write(text)
                Writer.Close()

                'Close this program\ form...
                'Me.Close()

            End If
        End If

    Next

    Me.Close()
End Sub

4

1 回答 1

0
Dim TARGET_FILE() As String 'This represent an array of string

File.ReadAllText

 System.IO.StreamWriter

不接收字符串数组作为参数

您应该迭代您的TARGETFILE数组集合并对ReadAllText每个文件执行以下操作:

 For Each File As String In TARGETFILE
         Dim ReadedText as string = System.IO.File.ReadAllText(File)
         Dim writer As New System.IO.StreamWriter("DestinationPath")
         writer.Write(ReadedText )
         Write.Close()
 Next
于 2013-05-22T09:46:13.790 回答