-1

I am reading a file in my winform and saving it in a list.i have a button "remove" and on clicking it an item (each list item is a line from the file) from the list is removeed and when i write back this list to a file the items i have removed are replaceed by a blank line.

I don't want these blank lines in my file. can anyone please tell me how to remove them.

I have tried using list.Remove(item) to remove the item from the list. here is what i have tried...

ListView.CheckedListViewItemCollection chkditems = listView1.CheckedItems;
            Regex regex1 = new Regex(".*\"(?<vm_name>.*)\".*:.*{.*\"vmx_path\".*:.*r?\"(?<vmx_path>.*)\",.*\"vm_base\".*:.*r?\"(?<vm_base>.*)\".*");
            List<string> list_to_items = new List<string>();
            foreach (ListViewItem chkitem in chkditems)
            {
                foreach (string line in list)
                {
                    Match match1 = regex1.Match(line);
                    if (match1.Success)
                    {
                        if (match1.Groups["vm_name"].Value == chkitem.Text)
                        {
                            list_to_items.Add(line);
                        }
                    }
                }
                listView1.Items.Remove(chkitem);
            }
            foreach (string tormv in index)
            {
                list.Remove(tormv);
            }

for sample data for the list you can consider it containing any text.

4

1 回答 1

1

应该是评论,但是用代码,很难……

我不知道你在做什么,但如果你运行这段代码,你会发现你的问题的假设是不正确的:

var list = new List<string>{"1","2","3"};
Console.WriteLine(string.Join(", ", list)); // 1, 2, 3
list.Remove("2");
Console.WriteLine(string.Join(", ", list)); // 1, 3
于 2013-09-26T09:36:01.673 回答