Tatia Posted October 11, 2013 at 06:57 PM Report #528584 Posted October 11, 2013 at 06:57 PM Boas amigos estou a tentar perceber como faço para substituir um valor de um arquivo pelo valor que coloco no textbox mas não sei como pesquisar para procurar a solução se alguém me poder indicar agradeço. Exemplo: Valor do arquivo Exemplo: gostava que ao colocar o valor neste textbox ele fica-se assim: Se alguém poder ajudar agradeço.
Tatia Posted October 12, 2013 at 01:04 AM Author Report #528631 Posted October 12, 2013 at 01:04 AM Para ler onde se encontra o valor ja consegui da seguinte forma. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim ID1 As New BinaryReader(File.OpenRead("c:\arquivo.bin")) Dim numHex As String = Nothing Dim nomeHex As String = Nothing Dim ValorHex As String = Nothing For i As Integer = &H1AA8B0 To &H1AA8BF ID1.BaseStream.Position = i numHex = ID1.ReadByte().ToString("X2") nomeHex += ChrW(Convert.ToInt64(numHex, 16)) ValorHex += nomeHex Next ID1.Close() Dim ID2 As New BinaryReader(File.OpenRead("c:\arquivo.bin")) For i As Integer = &H1AA898 To &H1AA8A7 ID2.BaseStream.Position = i numHex = ID2.ReadByte().ToString("X2") nomeHex += ChrW(Convert.ToInt64(numHex, 16)) ValorHex += nomeHex Next TextBox2.Text = nomeHex ID2.Close() End Sub Porem não sei como faço para mandar o valor da textbox para o arquivo substituindo o actual valor.
Tatia Posted October 13, 2013 at 04:09 AM Author Report #528718 Posted October 13, 2013 at 04:09 AM Malta ainda ando a tentar perceber como posso fazer alguém por favor poderia deixar alguma dica bem explicada pra mim?
nelsonr Posted October 13, 2013 at 10:58 AM Report #528738 Posted October 13, 2013 at 10:58 AM Boas Tatia, e onde defines qual a posição do conteúdo a alterar? Nesse exemplo que mostras, estás a substituir conteúdo em dois sitios, apenas indicando um string. 1 Report
Tatia Posted October 13, 2013 at 11:31 AM Author Report #528744 Posted October 13, 2013 at 11:31 AM (edited) o original tem os seguintes valores. Eu queria colocar no textbox1 por exemplo A2A2A2A2A2A2A2A2 Para alterar o primeiro valor Depois em textbox2 colocaria: B2B2B2B2B2B2B2B2 e ao clicar em um botao ele altera-se somente esses valores. No arquivo original o primeiro valor localiza-se no offset: 1AA898 e termina no 1AA8A7 o segundo valor começa no 1AA8B0 e termina 1AA8BF Ja tentei como se faz com o texto e ate deu mas fica corrompido o arquivo... Se eu alterar em editor hex tudo corre bem. Edited October 13, 2013 at 11:40 AM by Tatia
nelsonr Posted October 13, 2013 at 01:59 PM Report #528757 Posted October 13, 2013 at 01:59 PM Então mas a posição do conteúdo a alterar é fixa? 1 Report
Tatia Posted October 13, 2013 at 07:42 PM Author Report #528792 Posted October 13, 2013 at 07:42 PM (edited) Sim amigo é... Se facilitar vou deixar o ficheiro que quero editar... Clica Aqui Edited October 13, 2013 at 08:40 PM by Tatia
nelsonr Posted October 13, 2013 at 09:42 PM Report #528811 Posted October 13, 2013 at 09:42 PM Se o ficheiro que pretendes alterar não for muito grande (como nesse exemplo), podes ler o ficheiro todo para a memória, alterar e gravar novamente o conteúdo. Algo assim (está a gravar num ficheiro diferente para testes): ' Dim fileContent As Byte() = My.Computer.FileSystem.ReadAllBytes("D:\WORK\Lab\VBWindowsApplication1\file.bin") ' Dim position As Integer = &H1AA8B0 If TextBox1.Text.Length > 0 And position < fileContent.Length Then For Each c As Char In TextBox1.Text fileContent(position) = AscW(c) position = position + 1 If position > fileContent.Length Then Exit For End If Next ' My.Computer.FileSystem.WriteAllBytes("D:\WORK\Lab\VBWindowsApplication1\file2.bin", fileContent, False) End If 1 Report
Tatia Posted October 13, 2013 at 11:03 PM Author Report #528827 Posted October 13, 2013 at 11:03 PM Amigo és incrível era isso mesmo apenas modifiquei um pouco para usar apenas uma textbox... Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim fileContent As Byte() = My.Computer.FileSystem.ReadAllBytes("c:\file.bin") ' Dim position As Integer = &H1AA8B0 Dim position2 As Integer = &H1AA898 If TextBox1.Text.Length > 0 And position < fileContent.Length Then For Each c As Char In Mid(TextBox1.Text, 1, 16) fileContent(position) = AscW(c) position = position + 1 If position > fileContent.Length Then Exit For End If Next For Each c As Char In Mid(TextBox1.Text, 17, 16) fileContent(position2) = AscW(c) position2 = position2 + 1 If position2 > fileContent.Length Then Exit For End If Next ' My.Computer.FileSystem.WriteAllBytes("c:\file1.bin", fileContent, False) End If End Sub Muito obrigado mesmo pela ajuda não teria conseguido sem ti, andei 3 ou 4 dias toda a noite a fazer pesquisas e nunca consegui resolver isto... Obrigado.
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