4
WebClient webClient = new WebClient();
string mapPathName = Server.MapPath("\\images\\temp.jpg");
Uri uri = new Uri(mapLink);
webClient.DownloadFile(uri, mapPathName);
webClient.Dispose();
if (File.Exists(mapPathName))
    File.Delete(mapPathName);

I used the code above to download an encrypted image from Google Maps but after it was done downloading, I could not delete that file because it is being used. Can anybody please help me to solve this?

ahhhhhhhhhhhhhhh. very sorry you guys, my mistake. The code above worked but when i tried to draw before delete and this time it does not work. >_< here is the code :

PdfDocument document = new PdfDocument();
document.PageLayout = PdfPageLayout.SinglePage;
document.Info.Title = "Created with PDFsharp";

// Create an empty page
PdfPage page = document.AddPage();

// set size
page.Size = PageSize.A4;

// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);

WebClient webClient = new WebClient();
string mapPathName = Server.MapPath("\\images\\temp.jpg");
Uri uri = new Uri(mapLink);
webClient.DownloadFile(uri, mapPathName);

// defind position to draw the image
XRect rcImage = new XRect(x + 30, y, 410, 300);

// draw the image
gfx.DrawRectangle(XBrushes.Snow, rcImage);
gfx.DrawImage(XImage.FromFile(Server.MapPath("\\images\\temp.jpg")), rcImage);

// save pdf file
string filename = "_HelloWorld.pdf";
string filePath = Server.MapPath(filename);
if (File.Exists(filePath))
    File.Delete(filePath);

document.Save(Server.MapPath(filename));

gfx.Dispose();
page.Close();
document.Dispose();

if (File.Exists(mapPathName))
    File.Delete(mapPathName);
  • 1
    This is by design. If you want to delete a file, stop using it. – Jonathan Wood Jan 07 '13 at 03:14
  • 1
    @JonathanWood - where is it being used in the above code? – Preet Sangha Jan 07 '13 at 03:16
  • 2
    Why are you downloading it and immediately deleting it? Is this test code? – Maurice Reeves Jan 07 '13 at 03:21
  • @Maurice Reeves - yes this is just a test code. i am going to draw the image to a pdf file before i delete it. But i try to test deleting the image fisrt. – Solomon Kane Jan 07 '13 at 03:34
  • 2
    Have you considered the possibility that there's something like anti-virus or other such security software that's checking the code after the download to verify it's not a threat? I would consider temporarily disabling anti-virus and trying again to see if that's a culprit. – Maurice Reeves Jan 07 '13 at 03:46

2 Answers2

2

Is there a particular reason you're calling Dispose? This might be the cause. If you insist on using dispose, try to delete it before you call dispose. For example . . .

webClient.DownloadFileCompleted += (o, s) =>
{
    //if (File.Exists(mapPathName)) discard, see Paolo's comments below . . .
       File.Delete(mapPathName);
};
webClient.DownloadFileAsync(uri, mapPathName);
webClient.Dispose();

Also have you considered the using clause? This is usually prevents those kinds of errors from occurring.

rbtLong
  • 1,542
  • 3
  • 14
  • 31
  • The `DownloadFileCompleted` event is only invoked when using the asynchronous `DownloadFileAsync` method – Flagbug Jan 07 '13 at 03:28
  • Perhaps youre getting the error because it's still downloading and you're trying to delete it? – rbtLong Jan 07 '13 at 03:32
  • The `DownloadFile` method is a synchronous, blocking operation. There is no way that the file is still downloading after the method has finished. – Flagbug Jan 07 '13 at 03:34
  • 1
    Well that error usually occurs because it is still in the composition process. anyway, if it is a local file then you can always use the FileWatcher to see watch for changes until the file is free. http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/5695490c-a644-48a3-be2b-cfb762cf4765 – rbtLong Jan 07 '13 at 03:49
  • 2
    Using `File.Exists` in this case is redundant, since no exception is thrown from `File.Delete` if the file to be deleted does not exist. It's almost never appropriate to check if a file exists before trying to use it (the operation is not atomic). Check out [this question](http://stackoverflow.com/questions/673654/when-is-it-okay-to-check-if-a-file-exists) for more details. – Paolo Moretti Jan 07 '13 at 09:29
  • that is a very interesting consequence . . . – rbtLong Jan 07 '13 at 09:42
0

i solved my problem. insteading using

gfx.DrawImage(XImage.FromFile(Server.MapPath("\\images\\temp.jpg")), rcImage);

i divided it into 2 line

var img = XImage.FromFile(Server.MapPath("\\images\\temp.jpg"));
gfx.DrawImage(img, rcImage);

and then

img.Dispose;

then i can delete the image :D thank you all.