1

I am busy writing an additional module to an existing C# application. In my module I have to generate some data and add it to ZIP archives created by the main program. (Ionic.Zip library is used). In most cases I can successfully update existing archives and store them again. But sometimes (and quite often) this does not work, since, as far as I understand, something in the main program opens a stream to the concerned archive file and fails to close it before I try to access it. It is absolutely clear that the process that locks the files is the main program itself (tested with a tool based on How do I find out which process is locking a file using .NET?).

I am not aware of any technical possibility to trace down the point in the code where a file is opened without being (somewhere) closed, but it is my hope that such a technique may exist however and that someone could share some information on it.

TIA

Community
  • 1
  • 1
Alex Konnen
  • 717
  • 6
  • 20
  • Can you post the code that writes to the archive? Very hard to guess what's wrong with code otherwise. – Baldrick Jun 23 '16 at 08:34
  • Hi Baldrick, unfortunately the code of the main program is not written by me (I have the source code though). The writing occurs at dozens, maybe hundreds, of points. My hope (maybe naive) was that there existed a way to find out the place in the code where the restriction occurs. My precursor mostly did it within a class where he had class variables like ZipFile m_zipFileRead; ZipFile m_zipFileWrite; and operated with them to carry out the serialization. This class, again, is used at a plenty of places. – Alex Konnen Jun 23 '16 at 08:45
  • I don't think there's a substitute for stepping through the code in a case where it locks up and watching in Process Explorer / Process Monitor for when the stream handles are opened and closed. Make sure every stream is wrapped in a `using` statement. It'll probably take less time than you think. – Baldrick Jun 23 '16 at 09:07
  • It is not always `using()`, but I have checked (where I could) that whenever a stream gets opened, it is explicitly closed and disposed. To my knowledge this is equivalent to `using` - correct me if you think I am wrong. – Alex Konnen Jun 23 '16 at 09:11
  • Not if an exception is thrown... It can miss the close statement if anything fails. Wrapping in `using` guarantees closing even on exception – Baldrick Jun 23 '16 at 09:17
  • Baldrick, I must confess this is a good remark. Thanks for it. Naturally there can be an exception. But in this case try/catch/finally would guarantee it, and this is what I really used in all those cases wherever I did not use `using`. – Alex Konnen Jun 23 '16 at 09:28

0 Answers0