Jump to content

Recommended Posts

Posted

Boa Tarde,

Algum para ajudar nesta minha dúvida simples, que é:

No código seguinte como posso copiar da Linha 0 para a Linha 1 de um ficheiro de Texto.

//    
	
       Dim FILE As String = "c:\ficheirotxt.txt"
        Dim fluxoTexto As IO.StreamReader
        Dim linhaTexto As String
        Dim Linhas As String() = System.IO.File.ReadAllLines(FILE)
	        If IO.File.Exists(FILE) Then
            fluxoTexto = New IO.StreamReader(FILE)
            linhaTexto = fluxoTexto.ReadLine
            TextBox1.Text &= Linhas(0) & vbCrLf
            TextBox2.Text &= Linhas(1) & vbCrLf
            linhaTexto = fluxoTexto.ReadLine
            fluxoTexto.Close()
        Else
            MessageBox.Show("Arquivo não existe")
	        End If
	
Posted (edited)
Imports System.IO
Imports System.Text

Public Class Form1

    Private Function Read_File(ByVal fname As String) As String()
        Try
            Dim file_lines As New List(Of String)
            If (File.Exists(fname) = True) Then
                Using stream_file As New FileStream(fname, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite, 4096)
                    Using stream_reader As New StreamReader(stream_file)
                        While (stream_reader.EndOfStream = False)
                            file_lines.Add(stream_reader.ReadLine)
                        End While
                    End Using
                End Using
                Return file_lines.ToArray
            Else
                Return Nothing
            End If
        Catch IO_STATUS As Exception
            Return Nothing
        End Try
    End Function
    '
    Private Function Copy_Line(ByVal fname As String, ByVal xline As Integer, ByVal yline As Integer) As Boolean
        Dim lines As String() = Read_File(fname)
        lines(yline) = lines(xline)
        If (File.Exists(fname) = True) Then
            File.WriteAllLines(fname, lines)
            Return True
        Else
            Return False
        End If
    End Function
    '
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Copy_Line("C:\Users\Murilo\Desktop\metal_joiner.txt", 0, 1)
    End Sub

End Class

Vê se isso te serve, ele copia a linha 0 que na realidade é a primeira linha, para a segunda linha (line 1). Agora, por algum motivo eu esqueci que existia a função File.ReadAllLines(), então podes apagar a função read_file e usar ReadAllLines na função Copy_Lines, desculpe. Nem lembrava mais dessa função, mas o código funciona;

Edited by Muryllo
Posted (edited)

Sim era só isso, copiar da linha0 para a linha1.

Muito Obrigado, funcionou na perfeição.

Tinha feito muitas pesquisas e dificilmente chegava la.

Mais uma vez obrigado.

Edited by a3deluxe

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.