Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.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# - Fatal编程技术网

C# 使用程序后删除文件夹

C# 使用程序后删除文件夹,c#,C#,当我为一个文件夹的任何图片创建一个picturebox时,我创建了一个对话框程序,当我关闭这个对话框时,我不想删除图片框,但是,我收到一个异常,这个异常告诉我:文件0.jpg被另一个进程使用 但是,所以,我试着处理所有的图片盒。。。据我所知,我已经尝试了所有可能的事情,当然 因此,我的示例代码是: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin

当我为一个文件夹的任何图片创建一个picturebox时,我创建了一个对话框程序,当我关闭这个对话框时,我不想删除图片框,但是,我收到一个异常,这个异常告诉我:文件0.jpg被另一个进程使用

但是,所以,我试着处理所有的图片盒。。。据我所知,我已经尝试了所有可能的事情,当然

因此,我的示例代码是:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace delFolder
{
    public partial class FormPictureView : Form
    {
        private PictureBox pbSel = null;

        public FormPictureView()
        {
            InitializeComponent();

            AddPictureBox();
        }

        private void AddPictureBox()
        {
            int linha = 0;
            int x = 10, y = 10;
            string pngOutputPath = @"images\";

            string[] files = Directory.GetFiles(pngOutputPath);
            for (int i = 0; i < files.Length; i++)
            {
                if (linha == 2)
                {
                    x = 10;
                    y += 360;
                    linha = 0;
                }
                PictureBox pb = new PictureBox();
                pb.Name = string.Format("pb{0}", i);
                pb.Size = new Size(236, 321);
                pb.SizeMode = PictureBoxSizeMode.Zoom;
                pb.Image = Image.FromFile(string.Format("{0}{1}.jpg", pngOutputPath, i));
                pb.BackColor = Color.White;
                pb.Click += new EventHandler(pb_Click);
                pb.Tag = string.Format("{0}{1}.jpg", pngOutputPath, i);
                pb.Location = new System.Drawing.Point(x, y);
                panel1.Controls.Add(pb);
                x += 280;
                linha++;
            }
        }

        private void pb_Click(object sender, EventArgs e)
        {
            if (pbSel != null)
                pbSel.BorderStyle = BorderStyle.None;
            PictureBox pb = (PictureBox)sender;
            pb.BorderStyle = BorderStyle.FixedSingle;
            pbSel = pb;
            MessageBox.Show(pb.Tag.ToString());
        }
    }
}
并且,此对话框是主窗体的父级…,当我关闭此对话框时,我尝试删除图片框,但不可能:
如何解决此问题?

您需要在PictureBox中处理图像。

您需要在PictureBox中处理图像。

如果您设置了ImageLocation属性而不是Image属性no,请在删除图像之前将yourPictureBox.Image设置为null将获得文件锁定,您将能够删除该文件。但是,PictureBox将不再提供该图像,并且将显示无图像图标

要解决此问题,您可以在删除图像之前将其复制到内存中:

string imgPath = string.Format("{0}{1}.jpg", pngOutputPath, i);
// Retrieve image from file
Image img = Image.FromFile(imgPath);
// Create new canvas to paint the picture in
Bitmap tempImg = new Bitmap(img.Width, img.Height);
// Paint image in memory
using (Graphics g = Graphics.FromImage(tempImg))
{
   g.DrawImage(img, 0, 0);
}
// Assign image to PictureBox
pb.Image = tempImg;
// Dispose original image and free handles
img.Dispose();
// Delete the original file
File.Delete(imgPath);

如果设置ImageLocation属性而不是Image属性,则不会获得对文件的锁定,并且您将能够删除该文件。但是,PictureBox将不再提供该图像,并且将显示无图像图标

要解决此问题,您可以在删除图像之前将其复制到内存中:

string imgPath = string.Format("{0}{1}.jpg", pngOutputPath, i);
// Retrieve image from file
Image img = Image.FromFile(imgPath);
// Create new canvas to paint the picture in
Bitmap tempImg = new Bitmap(img.Width, img.Height);
// Paint image in memory
using (Graphics g = Graphics.FromImage(tempImg))
{
   g.DrawImage(img, 0, 0);
}
// Assign image to PictureBox
pb.Image = tempImg;
// Dispose original image and free handles
img.Dispose();
// Delete the original file
File.Delete(imgPath);