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
-
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
The secret is in reading the excepion text :P
toink toink wrote:
The process cannot access the file because it is being used by another process.
Which means someone already has a lock on that file. Possibly you. There are a few tools around which allow you to investigate the locks on a file. Theres a few at SysInternals[^] but personally I like Unlocker[^].