ADNSil Posted March 10, 2009 at 12:34 PM Report Share #249621 Posted March 10, 2009 at 12:34 PM boas tenho umas duvida e dificuldades relacionadas com ficheiros .txt... então é o seguinte: aaaaaaaa bbbbbbbb cccccccc dddddddd eeeeeeee ffffffffffff gggggggg hhhhhhhh - Imaginando que é esta a informação do .txt quero ler a informação a partir da linha "dddddddd" ate a linha "gggggggg" e apareça numa textbox, ignorando o que vem antes e depois. [resolvido] - Eliminar a informação a linha "dddddddd" ate a linha "gggggggg" - Gravar outra informação no lugar das linhas removidas (depois do ficheiro gravado ou reaberto) - Acrescentar informação a informação já existente(a seguir a linha "hhhhhhhh" acrescentar) usei o seguinte código como base, para ler a informação desejada ______________________________________________________________________________ Dim blnLerDados As Boolean = False Dim Texto As String = "" 'Cria um array, colocando o conteúdo de cada linha em indexs diferentes Dim Linha() As String = Strings.Split(My.Computer.FileSystem.ReadAllText("C:\teste.TXT", System.Text.Encoding.Default), ControlChars.CrLf) 'Verifica linha a linha For i As Integer = 0 To Linha.Length - 1 'Se não estiver vazia... If Linha(i) <> "" Then 'Lê o Conteúdo If (blnLerDados = True) And Not (Linha(i) = "gggggggg") Then Texto = Texto & " " & Linha(i) End If 'Verifica o conteúdo If (Linha(i) = "dddddddd") Then blnLerDados = True ElseIf (Linha(i) = "gggggggg") Then blnLerDados = False End If End If Next 'Escreve o texto na TextBox TextBox1.Text = Texto ________________________________________________________________________________ falta então: - apagar a informação desejada - guardar a informação no lugar da informação apagada - acrescentar informação Obrigado por a colaboração. Link to comment Share on other sites More sharing options...
Gooden Posted March 10, 2009 at 01:01 PM Report Share #249626 Posted March 10, 2009 at 01:01 PM Não devia ter feito isto mas toma fiz agora a pressão por tanto é provavel que tenha errozitos 😛 Public Class Form1 Public Remove_text As Integer = 1 Public replace_text As Integer = 2 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Debug.WriteLine("START PROGRAM") read("dddddddd", "gggggggg", "c:\teste.txt") Modify_File("c:\teste.txt", "C:\Tester.txt", Remove_text, "dddddddd", "Replacement") Modify_File("c:\teste.txt", "C:\Tester2.txt", replace_text, "dddddddd", "Replacement") End Sub Public Sub Modify_File(ByVal sFileName As String, ByVal Sfileoutput As String, ByVal Operation As Integer, ByVal Str1 As String, ByVal str2 As String) If IO.File.Exists(sFileName) Then Dim srFileReader As System.IO.StreamReader Dim sInputLine As String Dim watch As Boolean = False srFileReader = System.IO.File.OpenText(sFileName) sInputLine = srFileReader.ReadLine() Dim objStreamWriter As IO.StreamWriter = New IO.StreamWriter(Sfileoutput, True, System.Text.Encoding.Default) Do Until sInputLine Is Nothing If sInputLine = Str1 Then If Operation = 1 Then 'Remove line 'Como é para remover nao vou fazer nada apenas Imprimir em debug. Debug.Write("Remover Linha") ElseIf Operation = 2 Then 'Replace line 'Substituir ou seja envez de imprimir 1 imprime a outra. objStreamWriter.WriteLine(str2) Else 'remove 'Defeito é remover nao vou fazer nada apenas Imprimir em debug. Debug.Write("Remover Linha") End If Else objStreamWriter.WriteLine(sInputLine) End If sInputLine = srFileReader.ReadLine() Loop objStreamWriter.Close() Else MsgBox("Ficheiro não existe!") End If End Sub Public Sub read(ByVal startline As String, ByVal endline As String, ByVal sFileName As String) If IO.File.Exists(sFileName) Then Dim srFileReader As System.IO.StreamReader Dim sInputLine As String Dim watch As Boolean = False srFileReader = System.IO.File.OpenText(sFileName) sInputLine = srFileReader.ReadLine() Do Until sInputLine Is Nothing If sInputLine = startline Then watch = True End If If watch = True Then TextBox1.Text = TextBox1.Text & sInputLine & vbCrLf End If If sInputLine = endline Then watch = False End If sInputLine = srFileReader.ReadLine() Loop Else MsgBox("Ficheiro não existe!") End If End Sub End Class Link to comment Share on other sites More sharing options...
André Parreira Posted March 10, 2009 at 03:52 PM Report Share #249662 Posted March 10, 2009 at 03:52 PM Ja consegues saber onde esta certo? Entao agora fazes um if e guardas numa variavel o conteudo que queres. http://forum.codigofonte.net/index.php?showtopic=12549 ve se isso ajuda Link to comment Share on other sites More sharing options...
ADNSil Posted March 17, 2009 at 04:10 PM Author Report Share #251281 Posted March 17, 2009 at 04:10 PM já esta resolvido... de qualquer forma obrigado por tudo Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now