I've got a program that downloads a set of thumbnail images specified in a remote XML file. There can be up to 30 thumbnails downloaded. I'm using a foreach loop iterating through an XmlNodeList of all the <item> tags in the XML file and inside that foreach loop I start a new Thraed that runs the downloadThumb method. Here is my downloadThumb method:
Probably not the prettiest way, but it's the only way I could get it to write the thumbnail images properly to disk.
Anyway, I want the ./thumbs/ folder (with all the images inside) to be deleted when I close the program. I set up this in my .designer.cs file:
this.Closing += new System.Componen tModel.CancelEv entHandler(this .Form_Closing);
And my Form_Closing method:
dataGridView1 being the datagridview that I'm posting the information I'm parsing out of the XML file to. I figured that because I'm displaying the thumbnail I'm downloading inside a dataGridViewIma geCell that disposing the dataGridView would sever the association with the files...that's not the case. I need to know how I can disassociate the process with those files so I can delete them automatically. Can I get some help?
Code:
public void downloadThumb(object Url) { string url = Url.ToString(); Uri parts = new Uri(url); string query = parts.Query; string filename = query.Replace("?ssid=", ""); if(!File.Exists("./thumbs/" + filename + "-Thumbnail.jpg")) { HttpWebRequest thumbreq = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse thumbres = (HttpWebResponse)thumbreq.GetResponse(); BinaryReader thumb = new BinaryReader(thumbres.GetResponseStream()); FileStream fstream = new FileStream("./thumbs/" + filename + "-Thumbnail.jpg", FileMode.Create, FileAccess.Write); BinaryWriter file = new BinaryWriter(fstream); byte[] thumbnail = thumb.ReadBytes((int)thumbres.ContentLength); file.Write(thumbnail); file.Close(); thumb.Close(); fstream.Close(); thumbres.Close(); } }
Anyway, I want the ./thumbs/ folder (with all the images inside) to be deleted when I close the program. I set up this in my .designer.cs file:
this.Closing += new System.Componen tModel.CancelEv entHandler(this .Form_Closing);
And my Form_Closing method:
Code:
public void Form_Closing(object sender, CancelEventArgs cArgs) { dataGridView1.Dispose(); if (Directory.Exists("./thumbs")) { Directory.Delete("./thumbs", true); } }
Comment