Search code examples
c#replacestreamreaderreadlines

Read lines of Txt file, edit specific lines, then write full Txt file to new file - C#


I'd like to read a Txt file with parameters and values then update the values of specific parameters. I don't know the original value of the parameter before changing but have a list of parameter values that need changed. Writing the entire file with changed values to a new file is my goal.

I'm currently able to find the parameters, replace the lines with updated string, and write to a new file.

Structure of text file:

Parameter1=1.5
Parameter2=2.0
Parameter3=0.5

Output of new file when replacing Parameter1 and Parameter3:

Parameter1=0.7
Parameter3=3.0

Ideal output:

Parameter1=0.7
Parameter2=2.0
Parameter3=3.0
private string ProcessParameterUpdates(string selectedDPVFile, string parameterName, string parameterNameAndValue)
{
    List<string> dpvLines = new List<string>();
    string dpvLine;
    using (StreamReader reader = new StreamReader(selectedDPVFile))
    {
        string fileSelectedDirectory = Path.GetDirectoryName(selectedDPVFile);
        string outputFileName = Path.Combine(fileSelectedDirectory, "UpdatedParameters.dpv");

        while ((dpvLine = reader.ReadLine()) != null)
        {

            if (dpvLine.Contains(parameterName))
            {
                dpvLines.Add(dpvLine);
                dpvLine = dpvLine.Replace(dpvLine, parameterNameAndValue);
                if (File.ReadLines(outputFileName).Contains(parameterNameAndValue))
                {
                    writeWindow.SelectionColor = Color.DarkOrange;
                    writeWindow.AppendText("Parameter already updated: " + parameterNameAndValue + "\n");
                    Debug.WriteLine("Parameter already updated: " + parameterNameAndValue + "\n");
                    dpvLine = reader.ReadLine();
                    writeWindow.ForeColor = Color.Black;
                }
                else
                {
                    File.AppendAllText(outputFileName, dpvLine + "\n");
                    writeWindow.AppendText(dpvLine + "\n");
                    Debug.WriteLine(dpvLine + "\n");
                }
            }
        }
        reader.Close();
    }
    return selectedDPVFile;
}

Solution

  • Does this do the job for you?

    static void Main(string[] args)
    {
        string inputFile = @"e:\test.txt";
        string outputFile = @"e:\test2.txt";
    
        Dictionary<string, string> updates = new Dictionary<string, string>();
    
        for (int i = 0; i < args.Length; i++)
        {
            string[] parts = args[i].Split('=');
            string param = parts[0].Trim();
            string value = parts[1].Trim();
            updates[param] = value;
        }
    
        string[] lines = File.ReadAllLines(inputFile);
        List<string> updatedLines = new List<string>();
    
        foreach (string line in lines)
        {
            string[] parts = line.Split('=');
            string param = parts[0].Trim();
            string value = parts[1].Trim();
    
            if (updates.ContainsKey(param))
            {
                value = updates[param];
            }
    
            updatedLines.Add($"{param}={value}");
        }
    
        File.WriteAllLines(outputFile, updatedLines);
    }
    

    I assumed you will be passing params to the program as args. It should be easy to change to any source. You run the program as this:

    ConsoleApp1.exe Parameter1=0.7 Parameter3=3.0