1

目前我的表单有一个系统,我可以将表单中的数据保存到应用程序文件夹中的目录,然后通过我设置的列表框再次加载它现在我需要添加一个删除按钮来删除数据文件夹问题是如果我打开它返回错误无法删除因为文件正在使用这是我用来保存数据的代码

    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();
    }
4

3 回答 3

3

好的,所以问题PictureBox是使用您要删除的图像......对吗?因此,我们可以将图片加载到以下文件中,而不是使用本地驱动器中的图片PictureBox

public Bitmap LoadBitmap(string path)
{
    if (File.Exists(path))
    {
        // open file in read only mode
        using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
        // get a binary reader for the file stream
        using (BinaryReader reader = new BinaryReader(stream))
        {
            // copy the content of the file into a memory stream
            var memoryStream = new MemoryStream(reader.ReadBytes((int)stream.Length));
            // make a new Bitmap object the owner of the MemoryStream
            return new Bitmap(memoryStream);
        }
    }
    else
    {
        MessageBox.Show("Error Loading File.", "Error!", MessageBoxButtons.OK);
        return null;
    }
}

要使用它,只需调用:

picturebox1.Image = LoadBitmap("LOCATION");

现在您可以在不锁定的情况下删除图片。但请注意,`PictureBox 不会被清除。您必须手动清除它。

我希望它有所帮助。;)

于 2013-07-22T09:51:58.443 回答
1

Nathanael Jones 的博客文章“20 Image Resizing Pitfalls”准确描述了这个问题:

“打开文件名BitmapImage按文件名将导致文件在Bitmap实例期间被锁定。

这是他建议的解决方案:

“您可以通过使用 a 来避免锁定,FileStream将其克隆为 a MemoryStream,处置FileStream,然后使用该.Tag属性跟踪并处置MemoryStream后者。”

于 2013-07-22T10:03:20.857 回答
0

请改用 Load 方法。例如:pictureBox1.Load(图像的路径);通过使用这个你在关闭应用程序之前删除图像或文件夹没有问题希望这会有所帮助

于 2013-12-29T09:57:00.517 回答