Solved

Find specific text within a txt file?

Posted on 2014-03-06
6
334 Views
Last Modified: 2014-03-06
Hi,
I am creating a simple program to allow me to save my passwords in a text file.
I can create the file (is a plain text file), I can add new 'Concepts' and its 'Password' but now I need to search for an specific 'Concept' and display its password.

In the code below, "recordatorio" corresponds to the concept, and "clave_recordatorio" corresponds to its password.

The code I have, until now, is the following:

Code to Save new 'Concepts' with their 'Password'
    Private Sub btnGuardarClave_Click(sender As Object, e As EventArgs) Handles btnGuardarClave.Click
        'agregamos informacion al archivo contenedor de claves
        Dim ruta2 As String = CStr(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))
        Dim archivo2 As String = "archivo.txt"
        Dim addinfo As New System.IO.StreamWriter(ruta2 & "\" & archivo2, True)

        Dim recordatorio As String = CStr(txtRecordatorio.Text)
        Dim clave_recordatorio As String = CStr(txtClaveRecordatorio.Text)

        addinfo.WriteLine("[Recordatorio]: " & recordatorio)
        addinfo.WriteLine("[Clave]: " & clave_recordatorio)

        addinfo.Close()
        MsgBox("Archivo actualizado")
        Me.Close()
    End Sub

Open in new window

The result of the text file Is as follows:

[Recordatorio]: Telefonica
[Clave]: zxcvb
[Recordatorio]: VTR
[Clave]: 1234
[Recordatorio]: Computador Casa
[Clave]: asdf
[Recordatorio]: Computador Trabajo
[Clave]: qwerty

With this text file, if I want to know the password of 'Computador Casa', the program should say 'asdf'.

Code to Read the password for a 'Context':
No code yet, I have the form with 2 textboxes, one for the Concept (txtConcept), and the other for the Password (txtPassword) (read from the text file).
0
Comment
Question by:José Perez
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 25

Accepted Solution

by:
SStory earned 250 total points
ID: 39909598
#1: I would NEVER store passwords in a plain text file. You should think of encrypting the data at least.

#2 If I were saving this I'd do username,password or username tab password on each line:
  addinfo.WriteLine(string.format("{0}{1}{2}",recordatorio,vbTab,clave_recordatorio))

Then when reading it in. You just do the reverse.

        Dim Parts() As String
        Dim strLine As String
        Dim sr As System.IO.StreamReader = My.Computer.FileSystem.OpenTextFileReader("yourfilename")
        While sr.Peek <> -1
            strLine = sr.ReadLine()
            Parts = strLine.Split(vbTab)
            MsgBox(String.Format("Recordatorio: {0}  Clave {1}",Parts(0),Parts(1)
        End While

        sr.Close()

Open in new window


Obviously replace the "yourfilename" with your path as you did when writing it.
In the real world you'd want to try/catch/finally and be sure to clean up, handle exceptions and close.

Espero que le ayuda. Dios le bendiga.
0
 
LVL 2

Author Comment

by:José Perez
ID: 39910138
A propósito, Parts(0) y Parts(1) están, ambos, guardando "[Recordatorio: ] Telefonica", pero ninguno guarda la clave.

Inicialmente me arroja un error en 'vbTab', debo modificarlo y agregar 'CChar' como lo sugiera el VStudio?
Parts = strLine.Split(CChar(vbTab))

Open in new window


Finalmente, si agrego el 'CChar' corre peroarroja un error 'Index was outside the bounds of the array'

Que Dios te bendiga, también.
0
 
LVL 2

Author Comment

by:José Perez
ID: 39910171
Te adjunto una imagen del archivo y del mensaje que obtengo de los cambios que me indicaste...
0
Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

 
LVL 2

Author Comment

by:José Perez
ID: 39910311
Fixed!
This is how finally I solved:
        Dim Parts() As String
        Dim strLine As String

        'agregamos informacion al archivo contenedor de claves
        Dim ruta3 As String = CStr(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))
        Dim archivo3 As String = "archivo.txt"

        Dim sr As System.IO.StreamReader = My.Computer.FileSystem.OpenTextFileReader(ruta3 & "\" & archivo3)
        While sr.Peek <> -1
            strLine = sr.ReadLine() 'leemos linea del archivo
            Parts = strLine.Split(CChar(vbTab))
            If Parts(0) = CStr(txtRecordatorio.Text) Then
                'MsgBox(String.Format("Recordatorio: {0}  Clave {1}", Parts(0), Parts(1)))
                txtClave.Text = Parts(1)
            End If
        End While

        sr.Close()
    End Sub

Open in new window

0
 
LVL 2

Author Closing Comment

by:José Perez
ID: 39910317
I made some arrangements to what was indicated by the developer.
0
 
LVL 25

Expert Comment

by:SStory
ID: 39911352
Lo siento a veces escribimos codigo de la mente sin probarlo y faltemos una cosita.

Me alegre que le ayude.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question