I'm guessing that you're trying to replace a line of text in a text file. There are two ways to do that.
Option 1:
Read the whole file into memory. Replace the line in the data you've read. Write the whole file out again.
private void btnSättIn_Click(object sender, EventArgs e)
{
string prefix = LoggaIn.användarnamn + "," + LoggaIn.lösenord + ",";
bool found = false;
string[] lines = File.ReadAllLines("Konto.txt");
for (int index = 0; index < lines.Length; index++)
{
if (lines[index].StartsWith(prefix))
{
string[] parts = lines[index].Split(',');
double oldValue = double.Parse(parts[2]);
double belopp = double.Parse(txtBelopp.Text);
double newValue = oldValue + belopp;
parts[2] = newValue.ToString();
lines[index] = string.Join(",", parts);
found = true;
break;
}
}
if (found)
{
File.WriteAllLines("Konto.txt", lines);
}
else
{
}
}
File.ReadAllLines Method (System.IO) | Microsoft Docs[
^]
File.WriteAllLines Method (System.IO) | Microsoft Docs[
^]
Option 2:
If the file is too large to read into memory, you'll need to copy it line-by-line to another file, and then replace the original:
private void btnSättIn_Click(object sender, EventArgs e)
{
string prefix = LoggaIn.användarnamn + "," + LoggaIn.lösenord + ",";
bool found = false;
using (var reader = new StreamReader("Konto.txt"))
using (var writer = new StreamWriter("Konoto.tmp"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (found || !line.StartsWith(prefix))
{
writer.WriteLine(line);
}
else
{
string[] parts = line.Split(',');
double oldValue = double.Parse(parts[2]);
double belopp = double.Parse(txtBelopp.Text);
double newValue = oldValue + belopp;
parts[2] = newValue.ToString();
writer.WriteLine(string.Join(",", parts));
found = true;
}
}
}
if (found)
{
File.Delete("Konoto.txt");
File.Move("Konoto.tmp", "Konoto.txt");
}
else
{
File.Delete("Konoto.tmp");
}
}
NB: As I mentioned in the comments, it looks like you're storing a list of usernames and plain-text passwords in this text file. This is a very bad idea, and is not suitable for any real-world application. You should only ever store a salted hash of the user's password; and you should really be using a database instead of a text file.