由于文件名问题,vb.net可以读取\打开或保存文件

时间:2013-05-22 09:43:05

标签: vb.net getfiles

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

如果发现这些文件中的任何一个文件不包含“索引...”文本,则附加该特定文件以包含文档“C:\ stuff.txt”中找到的文本,然后使用更改保存文件...

我遇到的问题是文件名。

第一个问题是我的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

1 个答案:

答案 0 :(得分: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