I created the file:
FileInfo arq = new FileInfo("C:\ProgramData\dataMinGSMFans");
if (arq.Exists == false)
File.Create("C:\ProgramData\dataMinGSMFans").Close();
In a form I will see if it has content and assign this content to form objects (as in this case I got the color):
StreamReader file = new StreamReader(@"C:\ProgramData\dataMinGSMFans");
while ((line = file.ReadLine()) != null)
{
if (line.Contains("c1-"))
cor1.BackColor = Color.FromName(line.Replace("c1-", "").Trim());
else if (line.Contains("c2-"))
cor2.BackColor = Color.FromName(line.Replace("c2-", "").Trim());
}
file.Close();
To save to the file I'm using different parameters, such as a key to identify what I'm writing, such as c1-
and c2-
, cor1 and cor2:
var conteudo = string.Format("c1-{0}{1}", corPri.Color.Name, Environment.NewLine);
File.WriteAllText(@"C:\ProgramData\dataMinGSMFans", conteudo);
.
.
.
var conteudo = string.Format("c2-{0}{1}", corSec.Color.Name, Environment.NewLine);
File.WriteAllText(@"C:\ProgramData\dataMinGSMFans", conteudo);
The problem is: You are always recording on the first line of the file, and erasing the other one I had written, I want to know how to write content in an empty line of the file without deleting the file all, and if you already have a line of the parameter c1-
or c2-
(cor1 and cor2) it will replace the existing line by changing only the value after -