?
Solved

Unable to delete Package File after an exception during opening

Posted on 2011-10-17
6
Medium Priority
?
232 Views
Last Modified: 2013-12-16
I am creating a package file and fill content in it. For some reason, the package will get corrupt.

I wanted to handle this in this way:

try
{
  using (Package archivePkg = Package.Open("TestLogFile.zip", FileMode.OpenOrCreate))
  { ...
  } //end of using
} //end of try
catch (IndexOutOfRangeException ex)
{
File.Delete("TestLogFile.zip"); // IOException
}
//also at this point I am not able to delete the file

The exception of a corrupt zip file is caught but I am not able to delete the file: It is occupied by another process (my program!). I tested a lot of variants with using / try/catch but without success!
0
Comment
Question by:Mike2010
  • 4
  • 2
6 Comments
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36979273
Hello, what if you try this?:
try
{
    using (System.IO.FileStream fs = System.IO.File.Open("TestLogFile.zip", FileMode.OpenOrCreate))
    {
        using (Package archivePkg = Package.Open(fs, FileMode.OpenOrCreate))
        {
            // Etc...
        }
    }
}
catch (IndexOutOfRangeException ex)
{
    if (System.IO.File.Exists("TestLogFile.zip"))
        System.IO.File.Delete("TestLogFile.zip");
}

Open in new window

0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36979284
BTW, is strange that your file is being corrupt, check that only one thread/process is writing on it.
0
 

Author Comment

by:Mike2010
ID: 36979452
Thank you for your answers!

The file is existing (I can see the lock with MS Process Explorer), the test program was written as a windows console application so there is only one thread.
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 2000 total points
ID: 36979467
Ok, have you tried the posted code? still the same problem?
0
 

Author Comment

by:Mike2010
ID: 36979533
Sorry, I did not look sharp to your code: Your suggestion is right: If you are separating the file open and Package.Open process then I am able to delete the file.

Very clever, thank you!
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36979559
Glad to help, that means there's a bug on the Package.Dispose method, this must guarantee the release of all the resources, but it's not doing this.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question