Click here to Skip to main content
15,879,068 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

i run the below code, sometimes its' showing error like

'the process cannot find file '\path' it is used by another process'


can anyone tell me any problem in my code.


// code for Zip a multiple files//


Crc32 crc = new Crc32();
string attach = AppDomain.CurrentDomain.BaseDirectory + "Documents/" + "Attachments.zip";

if (File.Exists(attach))
{
    File.Delete(attach);
}

ZipOutputStream s = new ZipOutputStream(File.Create(attach));
s.SetLevel(9); // 0 - store only to 9 - means best compression



string names = hidfield.Value;
string[] strNames = names.Split(',');

for (int i = 0; i < strNames.Length; i++)
{

    FileStream fs = File.OpenRead(AppDomain.CurrentDomain.BaseDirectory + "Documents/" + strNames[i]);

    byte[] buffer = new byte[fs.Length];
    fs.Read(buffer, 0, buffer.Length);

    ZipEntry entry = new ZipEntry(ZipEntry.CleanName("Documents/" + strNames[i]));
    entry.DateTime = DateTime.Now;
    entry.Comment = " ";
    entry.ZipFileIndex = i;
    entry.Size = fs.Length;
    fs.Close();
    crc.Reset();
    crc.Update(buffer);
    entry.Crc = crc.Value;
    s.PutNextEntry(entry);
    s.Write(buffer, 0, buffer.Length);
}

s.Finish();
s.Close();
Posted
Updated 15-Dec-09 21:23pm
v3

1 solution

Why are you asking this over and over again ? Which line gives the error ? If it only happens sometimes, what is different when it happens ? Does it happen when you've been debugging, might you have stopped the session while the file was open ? Does it happen when you are running the program only outside the dev environment ? There's an app called wholockme or something, which you can find with google, which will show you what process is locking the file. That will help you tell if it's the current instance, or if it's a former instance that's still alive.

I see you didn't fix the other messes in this code that I pointed out to you. :(

Oh - is this an ASP.NET page ? I notice you're grabbing values from a hidden field. It would help if you tagged your question as ASP.NET, or told us it was ASP.NET, so we could actually help you. If this is a webpage, then of COURSE you will get this issue intermittently. If you have two page instances trying to write to the same file, what do YOU think will happen ?

Instead of asking the same questions over and over, try asking them right in the first place, and actually giving us the information we need to help you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900