0

I'm trying to write a ip and port to a text file from user input. However the code below only saves part of the created string.

The code I use to write:

private void Edit_Config(string data)
{
    if (File.Exists(file_loc))
    {
        using (StreamWriter edFile = new StreamWriter(file_loc, false))
        {
            edFile.WriteLine(data);
        }
    }
    else
    {
        //File not found, creates new config with new data.
        using (FileStream fs = File.Create(file_loc))
        {
            Byte[] bytes = new UTF8Encoding(true).GetBytes(data);
            fs.Write(bytes, 0, bytes.Length);
        }
    }
}

The code I use to call the command

Edit_Config(txt_serverIP.Text + ":" + txt_port.Text);

What I get in return for example if I put '192.168.2.60:8080' I get '192.168.2.6' saved.

                edFile.Close();

Closing the file fixed the issue.

1 Answers1

2

As long as your data string is correct, this should handle everything you're doing in that method right now:

private void Edit_Config(string data)
{
    File.AppendAllText(file_loc, data);
}

This should work in embedded framework:

private void Edit_Config(string data)
{
    using (var writer = new StreamWriter(file_loc, true))
    {
       writer.WriteLine(data);
    }
}
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794