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. Cannot open file to write

Cannot open file to write

Scheduled Pinned Locked Moved C#
debugginghelp
5 Posts 3 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
    Toubou
    wrote on last edited by
    #1

    I did:

    StreamReader sr = new StreamReader(path);
    sr.ReadToEnd();
    sr.Close();

    StreamWriter sw = new StreamWriter(path);

    I got error "The process cannot access the file because it is being used by another process" Why this happen, I already closed the file When in debug mode, it sometimes successfully openned the file, sometimes failed

    M 1 Reply Last reply
    0
    • T Toubou

      I did:

      StreamReader sr = new StreamReader(path);
      sr.ReadToEnd();
      sr.Close();

      StreamWriter sw = new StreamWriter(path);

      I got error "The process cannot access the file because it is being used by another process" Why this happen, I already closed the file When in debug mode, it sometimes successfully openned the file, sometimes failed

      M Offline
      M Offline
      MatthewAnderson
      wrote on last edited by
      #2

      Try opening the file as a filestream, and then passing the filestream handle to the streamreader and writer.

      T 1 Reply Last reply
      0
      • M MatthewAnderson

        Try opening the file as a filestream, and then passing the filestream handle to the streamreader and writer.

        T Offline
        T Offline
        Toubou
        wrote on last edited by
        #3

        I changed it to

        FileStream fs = new FileStream(path, FileMode.Open);

        StreamReader sr = new StreamReader(fs);
        sr.ReadToEnd();
        sr.Close();

        StreamWriter sw = new StreamWriter(fs);

        and still doesn't work I did wrong somewhere?

        A M 2 Replies Last reply
        0
        • T Toubou

          I changed it to

          FileStream fs = new FileStream(path, FileMode.Open);

          StreamReader sr = new StreamReader(fs);
          sr.ReadToEnd();
          sr.Close();

          StreamWriter sw = new StreamWriter(fs);

          and still doesn't work I did wrong somewhere?

          A Offline
          A Offline
          Anthony Mushrow
          wrote on last edited by
          #4

          all I can think of is maybe making sr = null; after you close it. I can't check right now though so...

          1 Reply Last reply
          0
          • T Toubou

            I changed it to

            FileStream fs = new FileStream(path, FileMode.Open);

            StreamReader sr = new StreamReader(fs);
            sr.ReadToEnd();
            sr.Close();

            StreamWriter sw = new StreamWriter(fs);

            and still doesn't work I did wrong somewhere?

            M Offline
            M Offline
            MatthewAnderson
            wrote on last edited by
            #5

            FileStream fs = new FileStream(path, FileMode.Open); StreamReader sr = new StreamReader(fs); StreamWriter sw = new StreamWriter(fs); sr.ReadToEnd(); sr.Close(); sw.Close(); fs.Close(); Closing the streamreader before initializing the streamwriter causes a Stream was not writable. error. Keep the reader and writer open until you close the file.

            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