Exception handling
-
I m trying to open a zip file selected by user and extracting it.... It runs fine for the first time but when i again clicks on the open file button and selects a zip file. It throws an exception: The process cannot access the file "file.png" because it is being used by another process Note: file.png being one of the files extracted from the .zip file. I think the FileStream object is not releasing the file after opening it. I tried using f.Destroy() and f.close() but they didnt work. Also the code runs for the first time successfully and it creates an error when another .zip file is opened (using the openfiledialog box) after opening the first file successfully.
String filename = openFileDialog1.FileName;
ZipInputStream zis = null; FileStream f = new FileStream(filename, FileMode.Open); zis = new ZipInputStream(f ); ZipEntry ze=null; while ((ze = zis.GetNextEntry()) != null) { FileStream fs = null; fs =System.IO.File.Create("d:\\\\puzzlesolver\\\\pieces\\\\"+ze.Name); int size = 2048; byte\[\] data = new byte\[2048\]; while (true) { size = zis.Read(data, 0, data.Length); if (size > 0) { fs.Write(data, 0, size); } else { break; } } fs.Close(); } //f.Flush(); f.Close(); f.Dispose(); zis.Close(); zis.Dispose();
-
I m trying to open a zip file selected by user and extracting it.... It runs fine for the first time but when i again clicks on the open file button and selects a zip file. It throws an exception: The process cannot access the file "file.png" because it is being used by another process Note: file.png being one of the files extracted from the .zip file. I think the FileStream object is not releasing the file after opening it. I tried using f.Destroy() and f.close() but they didnt work. Also the code runs for the first time successfully and it creates an error when another .zip file is opened (using the openfiledialog box) after opening the first file successfully.
String filename = openFileDialog1.FileName;
ZipInputStream zis = null; FileStream f = new FileStream(filename, FileMode.Open); zis = new ZipInputStream(f ); ZipEntry ze=null; while ((ze = zis.GetNextEntry()) != null) { FileStream fs = null; fs =System.IO.File.Create("d:\\\\puzzlesolver\\\\pieces\\\\"+ze.Name); int size = 2048; byte\[\] data = new byte\[2048\]; while (true) { size = zis.Read(data, 0, data.Length); if (size > 0) { fs.Write(data, 0, size); } else { break; } } fs.Close(); } //f.Flush(); f.Close(); f.Dispose(); zis.Close(); zis.Dispose();
Hi, this is the most likely explanation: if you want write or delete access (anything other than read access) to a file that just got created (by yourself or someone else, does not matter), chances are you will find the file is being accessed by some other process, and your access is not granted. The other process very likely is some server code that is there to assist you somehow. Candidates are: - anti-virus software (Norton, McAfee, whatever) - indexing software (Google Desktop, MS Office, whatever) The common thing is these packages are looking all the time for new files, so they can inspect them. Microsoft is aware of the consequences; Windows Explorer will try rename and delete attempts up to five times (with one-second interval), and only reports failure if the action continues to fail for that time. The solution: 1. either use a different file name 2. or remove all background reader candidates (bad idea) 3. or implement the retry loop as Explorer has it (use a Windows.Forms.Timer for this) BTW: if all you need is Read, make sure to allow others to read as well, i.e. use File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read) If you are convinced something else is going on, add try-catch (you should have done that straight away, every time and as soon as you do some I/O), display the entire exception text and the file name, note down the line numbers, tell your IDE to show line numbers, and publish all the details here. :) :) locate the offending line.
Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
-
Hi, this is the most likely explanation: if you want write or delete access (anything other than read access) to a file that just got created (by yourself or someone else, does not matter), chances are you will find the file is being accessed by some other process, and your access is not granted. The other process very likely is some server code that is there to assist you somehow. Candidates are: - anti-virus software (Norton, McAfee, whatever) - indexing software (Google Desktop, MS Office, whatever) The common thing is these packages are looking all the time for new files, so they can inspect them. Microsoft is aware of the consequences; Windows Explorer will try rename and delete attempts up to five times (with one-second interval), and only reports failure if the action continues to fail for that time. The solution: 1. either use a different file name 2. or remove all background reader candidates (bad idea) 3. or implement the retry loop as Explorer has it (use a Windows.Forms.Timer for this) BTW: if all you need is Read, make sure to allow others to read as well, i.e. use File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read) If you are convinced something else is going on, add try-catch (you should have done that straight away, every time and as soon as you do some I/O), display the entire exception text and the file name, note down the line numbers, tell your IDE to show line numbers, and publish all the details here. :) :) locate the offending line.
Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused: