0

this sounds overly complicated but i'm not too sure how to write that simpler so i'll wright a longer version.

Inside the text file i have this format saved (username, cash) and that repeats on every line. is if possible to find if the username already exists in the file, to add the cash from this turn (entered via text boxes) to the cash in the file for that specific user

after that i require code that searches the code for the username, then outputs the username and cash in separate text boxes

here is some of the code that i started from, but I've only managed to check if username is in the file so far, i haven't been able to do anything with it

        Dim vOut As String
        vOut = CStr(profit)
        Dim Findstring1 = IO.File.ReadAllText("profit.txt")
        Dim Lookfor As String = (Username)
        If Findstring1.Contains(Username) Then
            MsgBox(Findstring1)
        Else
            file = My.Computer.FileSystem.OpenTextFileWriter("profit.txt", True)
            file.Write(vOut)
            file.Write(", ")
            file.Write(Username)
            file.WriteLine("|")
            file.Close()
        End If

1 Answers1

1

You can use File.ReadLines and LINQ. To extract the UserName+Password use String.Split:

Dim query = From line In File.ReadLines("profit.txt")
            Where line.Contains(",")
            Select arr = line.Split(","c)
            Select (UserName:= arr(0).Trim(), Password:= arr(1).Trim())
        
For Each usernameAndPassword In query
    Dim txtUserNameText = usernameAndPassword.UserName
    Dim txtPasswordText = usernameAndPassword.PassWord              
Next
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • it says that "readlines" is not a member of "streamwriter" – Sokol TV Mar 30 '21 at 13:51
  • @SokolTV: i'm not using a StreamWriter. I'm just using `System.IO.File.ReadLines` – Tim Schmelter Mar 30 '21 at 13:52
  • also along with that valuetuple(of) is not defined or imported and "query" is not given a type – Sokol TV Mar 30 '21 at 13:52
  • Im not too sure, how can i check? – Sokol TV Mar 30 '21 at 13:56
  • Project > Settings > Application > Target Framework – Tim Schmelter Mar 30 '21 at 13:59
  • 1
    @SokolTV please see https://stackoverflow.com/a/38383064/1797425 it will resolve your error you're seeing. You're just using an older version of .NET, it's fine, but you need to install the missing libraries to support value tuple types. – Trevor Mar 30 '21 at 14:05
  • @SokolTV: I hope that you ensure that a username and a password cannot contain commas ;) – Tim Schmelter Mar 30 '21 at 14:09
  • is there no other way of doing this? when i put install it says "declaration expected" also yes don't worry the username cannot have commas – Sokol TV Mar 30 '21 at 14:13
  • @SokolTV: I don't know what code you are using but above works. In the loop you get all username/password combinations in this file and you can do with them what you want. – Tim Schmelter Mar 30 '21 at 14:19
  • oh no that code is fine i was talking about "Install-Package System.ValueTuple -Version 4.5.0" not working – Sokol TV Mar 30 '21 at 14:22
  • @SokolTV: you are using NuGet package manager? https://learn.microsoft.com/en-us/nuget/consume-packages/install-use-packages-visual-studio You just need to search it in Visual-Studio and then click install. – Tim Schmelter Mar 30 '21 at 15:07