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?
string
variable, then useReplase
, 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