Solved

Find specific text within a txt file?

Posted on 2014-03-06
6
333 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

734 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