0

I was creating a program in Windows forms and I wanted to save the username and the password in 2 separate files. One is named: name. The second is named: password. So first I check if the 2 files are empty but when I run the code it deletes the data on the files.

private void btn1_Click(object sender, EventArgs e)
{
    int count = 0;
    TextWriter name = new StreamWriter("C:\\Users\\skerd\\AppData\\Local\\Temp\\jvvhbbcp..txt");
    TextWriter psswd = new StreamWriter("C:\\Users\\skerd\\AppData\\Local\\Temp\\x1pu0bds..txt");

    if (new FileInfo("C:\\Users\\skerd\\AppData\\Local\\Temp\\jvvhbbcp..txt").Length == 0 && new FileInfo("C:\\Users\\skerd\\AppData\\Local\\Temp\\x1pu0bds..txt").Length == 0)
    {
        if (count < 1)
        {
            name.Write(txtB_1.Text);
            psswd.Write(txtB_2.Text);
            name.Close();
            psswd.Close();
            count++;
        }
        Close();
    }

Let's suppose I insert Carl in the name and Jones in the surname when I restart the program I want that in the files it got in the first one the name and in the second one the surname without deleting anything. Thanks!

T.S.
  • 18,195
  • 11
  • 58
  • 78
Skerdi Velo
  • 121
  • 2
  • 13
  • 5
    Would be much easier with [File.WriteAllText](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.writealltext) instead of the StreamWriter. – Klaus Gütter Aug 29 '22 at 13:49
  • 1
    The code you posted doesn't delete anything, it overwrites what's already in those files. You could do the same with just `File.WriteAllText(path,text)`. The code is leaky though and already generates warnings saying that the TextReader instances need to be disposed. – Panagiotis Kanavos Aug 29 '22 at 13:49
  • 2
    If you want to append text use the `Append` methods – Panagiotis Kanavos Aug 29 '22 at 13:50
  • 2
    Does this answer your question? [Append lines to a file using a StreamWriter](https://stackoverflow.com/questions/7306214/append-lines-to-a-file-using-a-streamwriter) – Slack Groverglow Aug 29 '22 at 13:52
  • 2
    If your goal is to persist some settings I would recommend taking a look at [user settings](https://learn.microsoft.com/en-us/dotnet/desktop/winforms/advanced/how-to-write-user-settings-at-run-time-with-csharp?view=netframeworkdesktop-4.8). That will give you an easy way to persist individual values between sessions, without having to do the reading/writing to files yourself. – JonasH Aug 29 '22 at 13:53
  • File.AppendAllText exists. It differs from File.WriteAllText in that it appends instead of replacing -- it adds the new text onto the end of the file without deleting what was already there. – Wyck Aug 29 '22 at 16:04

1 Answers1

1

You can load your files content or create new content and then modify your content and then - delete original and replace with new. Although, you could, as additional step, first - rename the original as backup and then save the new and then remove the original backup.

    var nameFile = "PATH1";
    var pwdFile = "PATH2";
    bool nameExists = File.Exists(nameFile); 
    bool pwdExists = File.Exists(pwdFile);
    
    StringBuilder nameContent = new StringBuilder();
    if (nameExists)
        nameContent.Append(File.ReadAllText(nameFile));
    
    
    StringBuilder pwdContent = new StringBuilder();
    if (pwdExists) 
        pwdContent.Append(File.ReadAllText(pwdFile));
    

    // you can potencially do more text manipulations here
    
    nameContent.AppendLine("NEW NAME");
    pwdContent.AppendLine("NEW PASSWORD");
    
    if (nameExists)
        File.Delete(nameFile);
    
    File.WriteAllText(nameFile, nameContent.ToString());
    
    
    if (pwdExists)
        File.Delete(pwdFile); 
    
    File.WriteAllText(pwdFile, pwdContent.ToString());
T.S.
  • 18,195
  • 11
  • 58
  • 78