目前我的表单有一个系统,我可以将表单中的数据保存到应用程序文件夹中的目录,然后通过我设置的列表框再次加载它现在我需要添加一个删除按钮来删除数据文件夹问题是如果我打开它返回错误无法删除因为文件正在使用这是我用来保存数据的代码
    private void button3_Click(object sender, EventArgs e)
    {
            using (var writer = new StreamWriter(@"cards\" + CardName.Text + ".card", false))
            {
                int lev = Level.SelectedIndex;
                int race = Race.SelectedIndex;
                int attribute = CardAttribute.SelectedIndex;
                int cardtype = comboBox1.SelectedIndex;
                int monstertype = comboBox2.SelectedIndex;
                int spelltype = comboBox4.SelectedIndex;
                int traptype = comboBox3.SelectedIndex;
                String[] des = CardDescription.Text.Split('\n');
                ImageConverter img_converter = new ImageConverter();
                byte[] bytes = (byte[])img_converter.ConvertTo(IMG, typeof(byte[]));  
                File.WriteAllBytes(@"cards\" + CardName.Text + ".jpg", bytes);
                writer.WriteLine("#ID:" + CardID.Text);
                writer.WriteLine("#lev:" + lev);
                writer.WriteLine("#rac:" + race);
                writer.WriteLine("#att:" + attribute);
                writer.WriteLine("#atk:" + ATK.Text);
                writer.WriteLine("#def:" + DEF.Text);
                writer.WriteLine("#pic:" + CardName.Text + ".jpg");
                writer.WriteLine("#ctp:" + cardtype);
                writer.WriteLine("#mtp:" + monstertype);
                writer.WriteLine("#stp:" + spelltype);
                writer.WriteLine("#ttp:" + traptype);
                writer.WriteLine("#gol:" + checkBox1.Checked);
                writer.WriteLine("#nam:" + CardName.Text);
                foreach (string l in des)
                {
                    writer.WriteLine("#des:" + l);
                }
            }
            ListUpdate();
        }
加载数据
    private void listBox1_DoubleClick(object sender, EventArgs e)
    {
        string open = @"cards\" + listBox1.SelectedItem.ToString() + ".card";
        var reader = new StreamReader(File.OpenRead(open));
        while (!reader.EndOfStream)
        {
            string line = reader.ReadLine();
            if (line.StartsWith("#ID:"))
            {
                CardID.Text = line.Substring(4);
            }
            if (line.StartsWith("#lev:"))
            {
                string lev = line.Substring(5);
                Level.SelectedIndex = Convert.ToInt32(lev);
            }
            if (line.StartsWith("#rac:"))
            {
                string rac = line.Substring(5);
                Race.SelectedIndex = Convert.ToInt32(rac);
            }
            if (line.StartsWith("#att:"))
            {
                string att = line.Substring(5);
                CardAttribute.SelectedIndex = Convert.ToInt32(att);
            }
            if (line.StartsWith("#atk:"))
            {
                ATK.Text = line.Substring(5);
            }
            if (line.StartsWith("#def:"))
            {
                DEF.Text = line.Substring(5);
            }
            if (line.StartsWith("#ctp:"))
            {
                string ctp = line.Substring(5);
                comboBox1.SelectedIndex = Convert.ToInt32(ctp);
            }
            if (line.StartsWith("#mtp:"))
            {
                string mtp = line.Substring(5);
                comboBox2.SelectedIndex = Convert.ToInt32(mtp);
            }
            if (line.StartsWith("#stp:"))
            {
                string stp = line.Substring(5);
                comboBox4.SelectedIndex = Convert.ToInt32(stp);
            }
            if (line.StartsWith("#ttp:"))
            {
                string ttp = line.Substring(5);
                comboBox3.SelectedIndex = Convert.ToInt32(ttp);
            }
            if (line.StartsWith("#gol:"))
            {
                string gold = line.Substring(5);
                checkBox1.Checked = Convert.ToBoolean(gold);
            }
            if (line.StartsWith("#nam:"))
            {
                CardName.Text = line.Substring(5);
            }
            if (line.StartsWith("#des:"))
            {
                des += line.Substring(5) + Environment.NewLine;
                CardDescription.Text = des;
            }
            if (line.StartsWith("#pic:"))
            {
                if (File.Exists(@"cards\" + line.Substring(5)))
                {
                    IMG = Image.FromFile(@"cards/" + line.Substring(5));
                }
            }
        }  
        des = "";
        GenerateCard();
    }
并删除它
    private void button4_Click(object sender, EventArgs e)
    {
        string list = listBox1.SelectedItem.ToString();
        if (File.Exists(@"cards\" + list + ".card"))
        {
            File.Delete(@"cards\" + list + ".card");
        }
        if (File.Exists(@"cards\" + list + ".jpg"))
        {
            File.Delete(@"cards\" + list + ".jpg");
        }
        ListUpdate();
    }