如何在编辑后关闭文本文件
本文关键字:文本 文件 编辑 | 更新日期: 2023-09-27 18:16:17
我在form application
中有一个文本文件installer_input.txt
和checkedListBox2
。如果我在checkesListBox2
中有一些变化,我想编辑文本文件。我有两部分代码,我知道这很长,但我需要一些帮助:
private void checkedListBox2_SelectedIndexChanged(object sender, EventArgs e)
{
string path = AppDomain.CurrentDomain.BaseDirectory.ToString();
var lin = (path + "config.ini").ToString();
var lines = File.ReadAllLines(lin);
string InstallerFile = lines.Where(txt => txt.Contains("IstallerFile="))
.Select(txt => txt.Split('=')[1].Replace("'"", "")).FirstOrDefault();
string pathTemp = @"C:'temp'";
string[] pathArr = InstallerFile.Split('''');
string[] fileArr = pathArr.Last().Split('''');
string fileArr1 = String.Join(" ", fileArr);
string installerfilename = string.Format("{0}{1}", pathTemp, fileArr1);
IEnumerable<string> inilines = File.ReadAllLines(installerfilename).AsEnumerable();
bool IsChecked = checkedListBox2.CheckedItems.Contains(checkedListBox2.SelectedItem);
else if (fileArr1.Equals("installer_input.txt"))
{
if (IsChecked && checkedListBox2.CheckedItems.Count != checkedListBox2.Items.Count)
inilines = inilines.Select(line => line == string.Format("#product.{0}", checkedListBox2.SelectedItem)
? Regex.Replace(line, string.Format("#product.{0}", checkedListBox2.SelectedItem), string.Format(@"product.{0}", checkedListBox2.SelectedItem))
: line);
else if (!IsChecked || checkedListBox2.CheckedItems.Count == checkedListBox2.Items.Count)
inilines = inilines.Select(line => (line == string.Format("product.{0}", checkedListBox2.SelectedItem))
? Regex.Replace(line, string.Format(@".*product.{0}", checkedListBox2.SelectedItem), string.Format(@"#product.{0}", checkedListBox2.SelectedItem))
: line);
if (checkedListBox2.CheckedItems.Count == checkedListBox2.Items.Count)
checkBox1.Checked = true;
else
checkBox1.Checked = false;
string strWrite = string.Join(Environment.NewLine, inilines.ToArray());
File.WriteAllText(installerfilename, strWrite);
}
}
第二个代码是:
private void checkBox1_CheckedChanged_1(object sender, EventArgs e)
{
CheckBox cb = sender as CheckBox;
SetAllItemsChecked(cb.Checked);
var installerLines = ReadInstallerLines();
SetAllProductsChecked(installerLines.ToList(), cb.Checked);
SaveInstaller(installerLines);
}
private void SetAllItemsChecked(bool check)
{
for (int i = 0; i < this.checkedListBox2.Items.Count; i++)
{
this.checkedListBox2.SetItemChecked(i, check);
}
}
private IEnumerable<string> ReadInstallerLines()
{
var lin = (path + "config.ini").ToString();
var lines = File.ReadAllLines(lin);
string InstallerFile = lines.Where(txt => txt.Contains("IstallerFile="))
.Select(txt => txt.Split('=')[1].Replace("'"", "")).FirstOrDefault();
string pathTemp = @"C:'temp'";
string[] pathArr = InstallerFile.Split('''');
string[] fileArr = pathArr.Last().Split('''');
string fileArr1 = String.Join(" ", fileArr);
string installerfilename = pathTemp + fileArr1;
string installertext = File.ReadAllText(installerfilename);
return File.ReadLines(pathTemp + fileArr1);
}
private void SetAllProductsChecked(IList<string> installerLines, bool check)
{
for (var i = 0; i < installerLines.Count; i++)
{
if (installerLines[i].Contains("product="))
{
installerLines[i] = check
? installerLines[i].Replace("#product", "product")
: installerLines[i].Replace("product", "#product");
}
if (installerLines[i].Contains("product."))
{
installerLines[i] = check
?installerLines[i].Replace("#product.", "product.")
: installerLines[i].Replace("product.", "#product.");
}
}
}
private void SaveInstaller(IEnumerable<string> installerLines)
{
var lin = (path + "config.ini").ToString();
var lines = File.ReadAllLines(lin);
string InstallerFile = lines.Where(txt => txt.Contains("IstallerFile="))
.Select(txt => txt.Split('=')[1].Replace("'"", "")).FirstOrDefault();
string pathTemp = @"C:'temp'";
string[] pathArr = InstallerFile.Split('''');
string[] fileArr = pathArr.Last().Split('''');
string fileArr1 = String.Join(" ", fileArr);
string installerfilename = pathTemp + fileArr1;
File.WriteAllLines(installerfilename, installerLines);
}
}
首先工作,我可以从列表中检查框,但是当我试图点击checkBox1时,我有下一个错误:The process cannot access the file 'C:'temp'installer_input.txt' because it is used by another process
。我怎样才能使程序工作?如何优化我的代码?
基本上一个线程/进程可以访问一个资源也就是文件在这个例子中,在一个实例中你可以做的是使用EventWaitHandle等待直到其他进程或线程占用了文件,就像这样:
EventWaitHandle waitHandle = new EventWaitHandle(true, EventResetMode.AutoReset, "SHARED_BY_ALL_PROCESSES");
waitHandle.WaitOne();
/* process file*/
waitHandle.Set();