---
title: "The process cannot access the file because it is being used by another process"
type: "forum-thread"
url: "https://www.webforum.nu/amne/dotnet/106083-the-process-cannot-access-the-file-because-it-is-being-used-by-another-process"
topic: ".NET"
topic_url: "https://www.webforum.nu/amne/dotnet"
author: "toha"
published: "2004-06-23T21:22:00.000Z"
updated: "2004-06-23T21:36:54.000Z"
replies: 1
views: 317
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/dotnet/106083-the-process-cannot-access-the-file-because-it-is-being-used-by-another-process"
---

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

## #1 — toha, 2004-06-23T21:22Z

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.

Permalänk: https://www.webforum.nu/p/106083

## #2 — icaaq, 2004-06-23T21:36Z

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

mv icaaq

Permalänk: https://www.webforum.nu/p/1374914

---

Tråden på webben: https://www.webforum.nu/amne/dotnet/106083-the-process-cannot-access-the-file-because-it-is-being-used-by-another-process
