-1

I have a C# win form application that sets a new image = to an image from a file. Like this:

var displayPicture = Image.fromFile(/*path*/);
PicBoxPictureDisplay.BackgroundImage = displayPicture;

But when I go to delete it with:

File.Delete(/*path*/);

I get: "The process cannot access the file [name of file used in .FromFile]

How can I delete the .FromFile image?

Edit: the following methods fail;

  1. PicBoxPictureDisplay.BackgroundImage.Dispose();
  2. PicBoxPictureDisplay.BackgroundImage = null;
  3. displayPicture.Dispose();
  • before deleting file, you should dispose displayPicture object – kgzdev Jun 12 '17 at 12:16
  • Could you please explain your action (why you want to that)? – SᴇM Jun 12 '17 at 12:17
  • You should remove it as the background in the picture box and explicitly remove the reference to it (dispose the object holding the image). – rickvdbosch Jun 12 '17 at 12:18
  • @IkramTurgunbaev I have tried displayPicture.Dispose(); Still fails. – Jacob Garner Jun 12 '17 at 12:18
  • @SeM I want to delete the image that I set for displayImage – Jacob Garner Jun 12 '17 at 12:18
  • @RickvandenBosch I have tried PicBoxPictureDisplay.backgroundImage = null; and displayPicture.Dispose(); Still fails – Jacob Garner Jun 12 '17 at 12:19
  • set PicBoxPictureDisplay.BackgroundImage=null, and then try to delete the image using File.Delete("filepath"), – Vicky S Jun 12 '17 at 12:20
  • @JacobGarner It would be nice if you add this kind of info beforehand next time, so other users don't take time to answer your question with stuff you already tried. – rickvdbosch Jun 12 '17 at 12:22
  • @JacobGarner The file or stream must be available for the duration of the Image instance, so you need to load your image then create new object and clone/copy it to new one. Then you wiill be allowed to delete. – SᴇM Jun 12 '17 at 12:23
  • Have a look at [Process Explorer](https://technet.microsoft.com/en-us/sysinternals/bb896653) and check which process is _actually_ locking your file. Maybe you also have some other process targetting the file (for instance if you just placed it there with a completely separate application?) – rickvdbosch Jun 12 '17 at 12:27
  • @RickvandenBosch I only have the image being used in the win form app. The file is in the Bin/Debug folder. – Jacob Garner Jun 12 '17 at 12:32

1 Answers1

0

as already mention you need to dispose the object. Specifically you need to call Dispose() on the BackgroundImage:

PicBoxPictureDisplay.BackgroundImage.Dispose();
System.IO.File.Delete("test.png");

this way the lock on the file will be realsed and you can delete it.

Setting PicBoxPictureDisplay.BackgroundImage = null; will not help because it does not remove the object from memory, that you once loaded. It will only remove the reference to it. The lock will still remain.

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • I tried that, Still fails. – Jacob Garner Jun 12 '17 at 12:23
  • then the problem is not cause by the code that you have posted. Because using your posted code, my solution works. I just tested it. Any other process that is using this file? do you use it anywhere else as a background image? – Mong Zhu Jun 12 '17 at 12:31
  • No, not that I'm aware of. – Jacob Garner Jun 12 '17 at 12:42
  • The only thing that I find different is that I use this to get the file path: public string GetFilePath(string fileName) { string path = ""; var pathToReturn = new FileInfo(fileName); path = pathToReturn.FullName; return path; } – Jacob Garner Jun 12 '17 at 12:47