2013-05-22 27 views
0

我想編輯我的代碼文件夾中打開所有文件ATC(包括任何子目錄中的)vb.net可以讀打開或保存因爲文件名問題的文件

如果這些文件被發現不要包含文本「索引...」,該特定文件被追加以包含在文件「C:\ stuff.txt」中找到的文本,然後保存該文件的更改...

我遇到的問題與文件名稱。

第一個問題是我的TARGET_FILE不會打開;第二個問題是我的TARGET_FILE不會保存?

任何人都可以幫忙嗎?我已經標有「< < ----問題#1 &」 < < ----問題#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 

回答

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 
+0

ERR現在即時通訊丟了,我已經取代了我對你的代碼的每個文件通過其所有的失控現在作爲sugested? –

+0

您必須使用foreach來讀取您從目錄中獲得的TARGETFILE集合中的每個文件。一旦進入對於每一個陳述你都可以做你需要的東西。我剛剛給你舉了一個例子 –