I have done some homework for you, and this is the code I came up with. There is still some work to be done with selecting folder, now you have to write paths, but this is not the thread of this thread. Mainly the app works like you wanted to (I hope).
On the form put these controls:
- textBox (for inseting path to the folders)
- comboBox (to select files - text files)
- richTextBox (where text is displayed)
- button (to save the changed file)
optional:
- labels over controls to specify what they are/do
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.ObjectModel;
namespace Nov26EditFiles
{
public partial class Form1 : Form
{
string folderPath;
string fileName;
int textLenght;
bool bSaved;
bool bJumpCode;
public Form1()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen;
this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
}
private void PopulatingComboBox()
{
try
{
this.comboBox1.Items.Clear();
this.richTextBox1.Text = null;
string[] allFilesPath = System.IO.Directory.GetFiles(@"" + folderPath, "*.txt");//, System.IO.SearchOption.AllDirectories);
foreach (string filePath in allFilesPath)
{
string file = System.IO.Path.GetFileName(filePath);
this.comboBox1.Items.Add(file);
}
int filesCount = allFilesPath.Length;
MessageBox.Show(filesCount + " files found in:\n" + folderPath, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch
{
MessageBox.Show("The written path is not valid.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (!bJumpCode)
{
try
{
string fileName2 = this.comboBox1.SelectedItem.ToString();
int textLenghtNew = this.richTextBox1.Text.Length;
if (!bSaved && textLenghtNew != textLenght)
{
if (DialogResult.Yes == MessageBox.Show("File " + fileName + " has …