编辑后如何关闭文本文件? [英] How I can close a text file after I edited?

查看:43
本文介绍了编辑后如何关闭文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单应用程序中有一个文本文件installer_input.txt和一个checkedListBox2.如果 checkesListBox2 中有一些更改,我想编辑文本文件.我有两部分代码,我知道很长,但我需要一些帮助:

I have a text file installer_input.txt and a checkedListBox2 in a form application. I want to edit the text file if I have some changes in checkesListBox2. I have two parts of code, I know that are so long, but I need some help :

 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 时出现下一个错误:进程无法访问文件 'C:\temp\installer_input.txt' 因为它被另一个进程使用.我怎样才能使程序正常工作?以及如何优化我的代码?

First works, I can check the boxes from the list, but when I try to make click on checkBox1 I have the next error: The process cannot access the file 'C:\temp\installer_input.txt' because it is used by another process. How I can make program to works? And how I can optimize my code ?

推荐答案

基本上,单个线程/进程可以访问资源,在这种情况下,您可以使用 EventWaitHandle 等待直到其他进程或线程像这样占用文件:

Basically a single thread/process can access a resource which is the file in this case at one instance what you can do is use EventWaitHandle to wait untill some other process or thread has occupied the file like this:

EventWaitHandle waitHandle = new EventWaitHandle(true, EventResetMode.AutoReset, "SHARED_BY_ALL_PROCESSES");
waitHandle.WaitOne();
/* process file*/
waitHandle.Set();

这篇关于编辑后如何关闭文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆