I am trying to make a cfg file editor (config for the switch) in which I need to change 2 values. The bottom line is to open the config (specify the path to the file), enter ip and milticast_ip into the textboxes and click "replace", then the program searches for the line containing the command part, deletes it and rewrites part of the command with the existing value from the textbox.

private void selectBtn_Click(object sender, RoutedEventArgs e) { Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog(); openFileDialog.Filter = "CFG files (*.cfg)|*.cfg"; if (openFileDialog.ShowDialog() == true) cfgPath.Text = openFileDialog.FileName; } private void replaceBtn_Click(object sender, RoutedEventArgs e) { string ipKomm = ipTxt.Text; if (cfgPath.Text != "") { string line = null; string line_to_delete = "config ipif System ipaddress"; using (StreamReader reader = new StreamReader(cfgPath.Text)){ using (StreamWriter writer = new StreamWriter(cfgPath.Text)){ while ((line = reader.ReadLine()) != null){ if (String.Compare(line, line_to_delete) == 0) continue; writer.WriteLine(line); } } } } else { MessageBox.Show("Выберите файл для редактирования!"); } 

First, I select the desired file, it is written as a path to the textbox, below there are 2 more textboxes in the form, in which I enter the necessary parameters and the replace button (replaceBtn_Click) - when I click, I check if there is a path in the textbox to the file, looking for the line to be deleted and I get an error in the string "StreamWriter writer = new StreamWriter (cfgPath.Text))" ... Tell me what I am doing wrong or how can I realize what I want to do?

  • .cfg file (or rather its contents) lay out for example, perhaps there is a simpler way! At least better. - Monomax
  • @Monomax hkar.ru/XP2X there is a lot of text in the config, mostly commands for the switch, here are some of them. there is such a line "config ipif System ipaddress IP ..." I need to just replace this ip + there is another command below that has a multicast ip. The point is that when the switch is flashed, it was possible to edit the config file faster and not search for the necessary parameters in it - Enotik Pataskun
  • to look for a black cat in a dark room, and all the more so because it is not there, the task is very complicated, so please lay out an example of the configuration file! In essence, your goal can be achieved. solving the following tasks: Read the file, find the desired line, change the line, overwrite ALL file with the changed data! - Monomax
  • @Monomax yadi.sk/i/jBJroW13X1mSFA link to config - Enacho Patasquin
  • Of course, I was hoping for xml, but you can work with it, in fact, you have a file with markup, where you have "#" the beginning of the section, the sections are separated by a new line. In this case there is a header and a sign of the end of the file. On the other hand, if the text to be replaced (or at least part of the text) is clearly known, and such text is unique (not repeated anywhere else), then the file can be read into the string variable, then use Replase , and then write back to the file. But this is possible if the text is unique, apparently, you will need to use the first part of this comment. - Monomax

0