0

So I had help in getting my code to work, turns out I had something missing in my Combobox control.

What I'm trying to achieve: I'm selecting a file via a drop-down list, once selected, my RichTextBox shows the contents of the entire file. I want to be able to change a line in here, using the RichTextBox, I then have a Save button, once I click this, I want my changes saved to the file I've selected via the drop-down list.

My code thus far:

public ConfigChanger()
{
    InitializeComponent();
    LoadconfigList();
    this.configList.SelectedIndexChanged += new System.EventHandler(this.configList_SelectedIndexChanged);
}

void LoadconfigList()
{
    configList.Items.Clear();
    string[] files = Directory.GetFiles(@"d:\monitors.d\");
    foreach (string file in files)
        configList.Items.Add(Path.GetFileNameWithoutExtension(file));
}

private void configList_SelectedIndexChanged(object sender, EventArgs e)
{
    string fileName = (string)configList.SelectedItem;
    string filePath = Path.Combine(@"d:\monitors.d\", fileName + ".yml");

    if (File.Exists(filePath))
        configReader.AppendText(File.ReadAllText(filePath));
    else
        configReader.Clear();
}

This is the part that I can't get to function properly

private void saveConfig_Click_1(object sender, EventArgs e)
{
    string fileName = (string)configList.SelectedItem;
    richTextBox1.AppendText(configReader.Text);
    System.IO.File.WriteAllText(@fileName);
}

I can't figure out how to just save the change I want. The change I want to make is only this line in the config file:

docMeta.DownAcknowledge: False

And change that to

docMeta.DownAcknowledge: True

And vice versa. Then save the file exactly as is.

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
Dude85
  • 41
  • 1
  • 5
  • @Dude85: What the `configReader` does? And how do you loading the file content to the `richTextBox1`? – Jackdaw Jun 17 '21 at 12:32
  • configList = Dropdown menu configReader = Richtextbox saveConfig = Button System.IO.File.WriteAllText(@fileName); throws the following error: No overload for method 'WriteAllText' takes 1 arguments Thus the file doesn't get saved. void LoadconfigList() is how i populate the dropdownlist – Dude85 Jun 17 '21 at 12:40

1 Answers1

1

The WriteAllText() method has two parameters: file to write to and the string to write to the file.
Try to make the following changes in your code:

private void configList_SelectedIndexChanged(object sender, EventArgs e)
{
    string fileName = (string)configList.SelectedItem;
    string filePath = Path.Combine(@"d:\monitors.d\", fileName + ".yml");

    richTextBox1.Clear();

    if (File.Exists(filePath))
    {
        richTextBox1.AppendText(File.ReadAllText(filePath));
    }   
}

private void saveConfig_Click(object sender, EventArgs e)
{
    string file = Path.Combine(@"d:\monitors.d\", configList.SelectedItem + ".yml");
    File.WriteAllText(file, richTextBox1.Text);
}

I would change the code above to keep full path and file name inside the combo-box item. Thereafter you don't need to operate with [disk:\directory\filename.ext] in several places in your code. This will make the code more integral.

For example, it might look like this:

public class FileItem // Class to encapsulate the file name and full path
{
    public string FileName { get; set; }
    public string FullPathName { get; set; }
}

Changes in your methods:

void LoadconfigList()
{
    IList<FileItem> fileList = new List<FileItem>();
   
    string[] files = Directory.GetFiles(@"d:\monitors.d\");
    foreach (string file in files)
    { 
        fileList.Add(new FileItem() { FileName = Path.GetFileNameWithoutExtension(file), FullPathName = file});
    }

    configList.Items.Clear();
    configList.DataSource = fileList;
    configList.DisplayMember = "FileName";
    configList.ValueMember = "FullPathName";
}

private void configList_SelectedIndexChanged(object sender, EventArgs e)
{
    if (configList.SelectedItem is FileItem file)
    {
        richTextBox1.Clear();
        if (File.Exists(file.FullPathName))
        {
            richTextBox1.AppendText(File.ReadAllText(file.FullPathName));
        }             
    }
}

private void saveConfig_Click(object sender, EventArgs e)
{
    if (configList.SelectedItem is FileItem file)
    {
        File.WriteAllText(file.FullPathName, richTextBox1.Text);
    }
}
Jackdaw
  • 7,626
  • 5
  • 15
  • 33