1

My code is

Dictionary<string,string> members = new Dictionary<string,string>();
//.. initialization of this dictionary

                using (StreamWriter file = new StreamWriter(pathToFile))
                    foreach (var entry in members)
                        file.WriteLine("[{0} {1}]", entry.Key, entry.Value);

I need to write this stuff after the first specified string that could be present in the file "testString". How can this be done easily?

EluciusFTW
  • 2,565
  • 6
  • 42
  • 59
curiousity
  • 4,703
  • 8
  • 39
  • 59
  • So you want to append to the file after the location of a certain string? – tomsv Jul 16 '15 at 10:29
  • file could continue after this string -(appending or pasting in the center - something like this) – curiousity Jul 16 '15 at 10:30
  • So do you want insert your new text at that location, but keeping the rest of the text already in the file? – tomsv Jul 16 '15 at 10:31
  • yes, the Insert is corect word – curiousity Jul 16 '15 at 10:32
  • If its a small file you can read all of it into a string, insert your text, and write the entire string back. If it is a large file you need to move the previous file, copy the text up to your "testString" from the old file to the new file, write your new data, then copy the rest of the text from the old file. – tomsv Jul 16 '15 at 10:37

3 Answers3

1

Do you need to insert data in the middle of the file (i.e. quote: "write this stuff after the first specified string that could be present in this file")?

Dictionary<string, string> members = new Dictionary<string, string>();

String lineToFind = "testString";

// Let's read the file up in order to avoid read/write conflicts
var data = File
  .ReadLines(pathToFile)
  .ToList();

var before = data
  .TakeWhile(line => line != lineToFind)
  .Concat(new String[] {lineToFind}); // add lineToFind

var after = data
  .SkipWhile(line => line != lineToFind)
  .Skip(1); // skip lineToFind

var stuff = members
  .Select(entry => String.Format("[{0} {1}]", entry.Key, entry.Value));

File.WriteAllLines(pathToFile, before
  .Concat(stuff)
  .Concat(after));
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

You could read everything from the file and store as a string. Then find the index of the specified substring and insert your new values. Then write back to the text file using:

using (StreamWriter file = new StreamWriter(pathToFile, false)){}
Kieran Quinn
  • 1,085
  • 2
  • 22
  • 49
0

Create a temporary file and write all data to it line by line, while iterating through all lines search for the specified string and insert your data where you find it. While minimizing the memory consumption as well.

Dictionary<string, string> members = new Dictionary<string, string>();
//.. initialization of this dictionary

string pathToFile = "";

string tempfile = Path.GetTempFileName();   //create a temp file
using (var writer = new StreamWriter(tempfile))
using (var reader = new StreamReader(pathToFile))
{
    //if file is not ended
    while (!reader.EndOfStream)
    {
        //get next line
        string line = reader.ReadLine();

        //write it to the "temp file"
        writer.WriteLine(line);

        //search in the line, if found, insert your data
        if (line.Contains("search what you need"))
        {
            foreach (var entry in members)
                writer.WriteLine("[{0} {1}]", entry.Key, entry.Value);
        }                    
    }
}

//overwrite the actual file with temp file
File.Copy(tempfile, pathToFile, true);

Acknowledgement: I used the logic of Jake as he answered here.

Community
  • 1
  • 1
Shaharyar
  • 12,254
  • 4
  • 46
  • 66