473,329 Members | 1,213 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,329 software developers and data experts.

The process cannot access the file because it is being used by another process.

I have these line of codes :

try
{
string fromDir = file.Substring(0, file.LastIndexOf("\\"));
System.IO.Directory.SetCurrentDirectory(fromDir);
if (!File.Exists(fileToZip))
{
throw new FileNotFoundException("The specified file " + fileToZip + " could not be found. Zipping aborderd");
}

FileStream StreamToZip = new FileStream(fileToZip, FileMode.Open, FileAccess.ReadWrite);
FileStream ZipFile = File.Create(zippedFile);
ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);

ZipEntry ZipEntry = new ZipEntry(fileToZip.Substring(fileToZip.LastIndexOf ("\\") + 1));
ZipStream.PutNextEntry(ZipEntry);

ZipStream.SetLevel(CompressionLevel);
if (password != null && !password.Equals(string.Empty))
ZipStream.Password = password;
byte[] buffer = new byte[BlockSize];
System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);
ZipStream.Write(buffer, 0, size);
try
{
while (size < StreamToZip.Length)
{
int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
ZipStream.Write(buffer, 0, sizeRead);
size += sizeRead;
}
}
catch (System.Exception ex)
{
throw ex;
}
ZipStream.Finish();
ZipStream.Close();
StreamToZip.Close();
}
catch (Exception ex)
{
throw ex;
}


The error occurs on this line

FileStream ZipFile = File.Create(zippedFile);

how am i going to resolve it? thanks
Sep 4 '07 #1
2 3396
Plater
7,872 Expert 4TB
Are you overwriting a file? And possibly have the old zip file open?
You also aren't closing the FileStream "ZipFile" that you create.
Sep 4 '07 #2
You can try if the file eis opened in exclusive mode.
Something like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. try
  3.                         {
  4.                             using (FileStream oStream = File.Open
  5.                                 (
  6.                                 fileName,
  7.                                 FileMode.Open,
  8.                                 FileAccess.Write,
  9.                                 FileShare.None
  10.                                 ))
  11.                             {
  12.                                 break;
  13.                             }
  14.                         }
  15.                         catch
  16.                               Thread.Sleep(OnCreatedEventThreadSleepInMilliSeconds);
  17.  
Sep 5 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Paul | last post by:
Hi, VB.NET is saying the file I am creating is in use by another process and won't complete its task of moving the file to the specified destination folder. Here is my code (the main bit...
0
by: Tim | last post by:
Hi This is the problem: Read BLOB data from SQL Server. Create an image on the HD and assign it to a picture box. Works fine the first time. This is a usercontrol on a tab. When I close the tab...
5
by: Mike Dole | last post by:
When I am updating a picturebox with an image (extracted) from an access database I keep getting a "The process cannot access the file "c:\foto1.jpg because it is being used by another process"...
1
by: Jurgen Oerlemans | last post by:
Hello, I perform several actions on a file: * copy "xxx.txt" from directory 1 to "file.txt" directory 2 * process "file.txt" which is in directory 2 * delete the file in directory 1 .... and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.