Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 编辑后如何关闭文本文件?_C#_Visual Studio 2012 - Fatal编程技术网

C# 编辑后如何关闭文本文件?

C# 编辑后如何关闭文本文件?,c#,visual-studio-2012,C#,Visual Studio 2012,我在表单应用程序中有一个文本文件installer\u input.txt和一个checkedListBox2。如果在checkesListBox2中有一些更改,我想编辑文本文件。我有两部分代码,我知道它们很长,但我需要一些帮助: private void checkedListBox2_SelectedIndexChanged(object sender, EventArgs e) { string path = AppDomain.CurrentDo

我在
表单应用程序中有一个文本文件
installer\u 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 checkedListBox2\u SelectedIndexChanged(对象发送方,事件参数e)
{
字符串路径=AppDomain.CurrentDomain.BaseDirectory.ToString();
var lin=(path+“config.ini”).ToString();
var lines=File.ReadAllLines(lin);
string InstallerFile=行。其中(txt=>txt.Contains(“IstallerFile=”))
.Select(txt=>txt.Split('=')[1]。替换(“\”,”).FirstOrDefault();
字符串pathTemp=@“C:\temp\”;
string[]pathArr=InstallerFile.Split('\\');
字符串[]fileArr=pathArr.Last().Split('\\');
string fileArr1=string.Join(“,fileArr);
string installerfilename=string.Format(“{0}{1}”,pathTemp,fileArr1);
IEnumerable inilines=File.ReadAllLines(installerfilename).AsEnumerable();
bool IsChecked=checkedListBox2.CheckedItems.Contains(checkedListBox2.SelectedItem);
else if(fileArr1.Equals(“installer\u input.txt”))
{
if(IsChecked&&checkedListBox2.CheckedItems.Count!=checkedListBox2.Items.Count)
inilines=inilines.Select(line=>line==string.Format(“#product.{0}”),选中ListBox2.SelectedItem)
?Regex.Replace(line,string.Format(“#product.{0}”,checkedListBox2.SelectedItem),string.Format(@“product.{0}”,checkedListBox2.SelectedItem))
:行);
如果(!IsChecked | | checkedListBox2.CheckedItems.Count==checkedListBox2.Items.Count),则为else
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))
:行);
if(checkedListBox2.CheckedItems.Count==checkedListBox2.Items.Count)
checkBox1.Checked=true;
其他的
checkBox1.Checked=false;
string strWrite=string.Join(Environment.NewLine,inilines.ToArray());
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);
        }
    }
private void checkBox1\u CheckedChanged\u 1(对象发送方,事件参数e)
{
复选框cb=发送方作为复选框;
SetAllItemsChecked(cb.已检查);
var installerLines=ReadInstallerLines();
SetAllProductsChecked(installerLines.ToList(),cb.Checked);
SaveInstaller(installerLines);
}
私有无效设置项已检查(布尔检查)
{
对于(int i=0;itxt.Contains(“IstallerFile=”))
.Select(txt=>txt.Split('=')[1]。替换(“\”,”).FirstOrDefault();
字符串pathTemp=@“C:\temp\”;
string[]pathArr=InstallerFile.Split('\\');
字符串[]fileArr=pathArr.Last().Split('\\');
string fileArr1=string.Join(“,fileArr);
字符串installerfilename=pathTemp+fileArr1;
string installertext=File.ReadAllText(installerfilename);
返回File.ReadLines(pathTemp+fileArr1);
}
私有void SetAllProductsChecked(IList安装线,布尔检查)
{
对于(var i=0;itxt.Contains(“IstallerFile=”))
.Select(txt=>txt.Split('=')[1]。替换(“\”,”).FirstOrDefault();
字符串pathTemp=@“C:\temp\”;
string[]pathArr=InstallerFile.Split('\\');
字符串[]fileArr=pathArr.Last().Split('\\');
string fileArr1=string.Join(“,fileArr);
字符串installerfilename=pathTemp+fileArr1;
writeAllines(installerfilename,installerLines);
}
}
首先,我可以选中列表中的复选框,但当我尝试单击复选框1时,我出现了下一个错误:
进程无法访问文件“C:\temp\installer\u input.txt”,因为它被另一个进程使用。

如何使程序工作?如何优化代码?

基本上,一个线程/进程可以访问一个资源,在本例中,该资源就是一个实例中的文件
EventWaitHandle waitHandle = new EventWaitHandle(true, EventResetMode.AutoReset, "SHARED_BY_ALL_PROCESSES");
waitHandle.WaitOne();
/* process file*/
waitHandle.Set();