Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Exception handling

Exception handling

Scheduled Pinned Locked Moved .NET (Core and Framework)
help
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    Umangj
    wrote on last edited by
    #1

    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();
    
    L 1 Reply Last reply
    0
    • U Umangj

      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();
      
      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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:


      U 1 Reply Last reply
      0
      • L Luc Pattyn

        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:


        U Offline
        U Offline
        Umangj
        wrote on last edited by
        #3

        Thank you so much... Your help made it work... i used

        FileStream fs;
        fs =System.IO.File.Create("d:\\puzzlesolver\\pieces\\"+ze.Name,2048,FileOptions.Asynchronous);

        and write the stream on to this file.... ;)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups