webForumDet fria alternativet

The process cannot access the file because it is being used by another process

1 svar · 317 visningar · startad av toha

tohaMedlem sedan dec. 1999707 inlägg
#1

Jag har ett litet problem. Jag har byggt en klass som inkapslar ett Bitmap. och ett FileInfo objekt.

public class Image : IDisposable
{
	internal Bitmap bitmap =				null;
	internal FileInfo fileInfo =			null;
	public Image(ZooGallery zooGallery, FileInfo fileInfo)
			{
				this.fileInfo =		fileInfo;
				this.zooGallery =	zooGallery;
				this.bitmap =		new Bitmap(fileInfo.FullName);
			}

	public void Dispose()
	{
		this.Dispose(true);
		GC.SuppressFinalize(this);
	}
	private void Dispose(bool disposing)
	{
		if(!this.disposed)
		{
			if(disposing)
			{
				this.bitmap.Dispose();
			}
		}
		disposed = true;         
	}
}

På min asp.net-sida har jag en DataGrid vars DataSource är en ArrayList med mina Bildobjekt.


	private void Page_Load(object sender, System.EventArgs e)
	{
		this.moduleId =	System.Convert.ToInt32(Request.QueryString["mid"]);
		this.gallery =	ZooGallery.Get(this.moduleId.ToString());
		if (this.gallery == null)
		{
			this.gallery =	ZooGallery.Create(this.moduleId.ToString());
		}
		if (!Page.IsPostBack)
		{
			BindImages();
		}
	}

	private void BindImages()
	{
		this.imageGrid.DataSource =	this.gallery.Images;
		this.imageGrid.DataBind();
	}

	private void imageGrid_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
	{
		string fileName =	e.CommandArgument.ToString();
		this.imageGrid.Dispose();
		this.imageGrid.DataSource =	null;
		this.gallery.DeleteImage(fileName);
		BindImages();
	}

Vid ett Update-event försöker jag göra en System.IO.File.Delete() på en av filerna som används i mitt objekt.

Aha tänkte jag. Jag måste göra Dispose() på mitt objekt innan så det släpper sitt Bitmap- och FileInfo-object. (I min Dispose-metod så gör jag det). Men det fungerar inte trots allt.

System.IO.IOException: The process cannot access the file
"C:\test.jpg" because it is being used by another process

Tips mottages med glädje.

icaaqMedlem sedan okt. 20005 273 inlägg
#2

Detta kanske kan hjälpa dig
http://www.webforum.nu/showthread.php?s=&postid=848675#post848675

mv icaaq

133 ms totalt · 3 externa anrop · v20260731065814-full.30151723
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
130 ms — hämta tråd, inlägg och bilagor (db)