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. File.Create method isn't releasing the file

File.Create method isn't releasing the file

Scheduled Pinned Locked Moved C#
help
3 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.
  • K Offline
    K Offline
    kornstyle
    wrote on last edited by
    #1

    I am using the File.Create method to create a text file. My problem is that when I need to write to that file after it has been created I get the error message "The process cannot access the file "C:\2005 Sim R4 MedAlu Data\TN.txt" because it is being used by another process.".

    I P 2 Replies Last reply
    0
    • K kornstyle

      I am using the File.Create method to create a text file. My problem is that when I need to write to that file after it has been created I get the error message "The process cannot access the file "C:\2005 Sim R4 MedAlu Data\TN.txt" because it is being used by another process.".

      I Offline
      I Offline
      Ingo
      wrote on last edited by
      #2

      You must derive a FileStream von the Create method: FileStream fs = File.Create("C:\2005 Sim R4 MedAlu Data\TN.txt"); After that you can access the FileStream: fs.Write("Your Data"); and finally close it. fs.Close(); -- modified at 9:49 Thursday 15th December, 2005

      1 Reply Last reply
      0
      • K kornstyle

        I am using the File.Create method to create a text file. My problem is that when I need to write to that file after it has been created I get the error message "The process cannot access the file "C:\2005 Sim R4 MedAlu Data\TN.txt" because it is being used by another process.".

        P Offline
        P Offline
        Polis Pilavas
        wrote on last edited by
        #3

        What code are you using? I suspect that you didn't call the Close() method right after you created the file. We can't do much without seeing the actual code you use. But anyways, here's a sample code I use when creating/writing text files:

        FileStream fs = null;
        string tmpFile = "C:\\sample.txt";

        // Create file and close it so that it can be re-opened for writing further on
        fs = File.Create(tmpFile, 1024);
        fs.Close();

        // Prepare file for writing by assigning a new textWriter to it
        TextWriter wr = new StreamWriter(tmpFile);

        // Write stuff into the file
        wr.WriteLine("Stuff 1"); wr.WriteLine("Stuff 2"); wr.WriteLine("Blah blah blah......");

        // Eventually close the file
        wr.Close();

        Regards, Polis Can you practice what you teach? -- modified at 9:53 Thursday 15th December, 2005

        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