Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. The process cannot access the file because it is being used by another process.

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

Scheduled Pinned Locked Moved C#
helpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    toink toink
    wrote on last edited by
    #1

    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

    O 1 Reply Last reply
    0
    • T toink toink

      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

      O Offline
      O Offline
      originSH
      wrote on last edited by
      #2

      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[^].

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups