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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Newly created file stays locked

Newly created file stays locked

Scheduled Pinned Locked Moved C#
csharpasp-nethelpquestionworkspace
3 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.
  • D Offline
    D Offline
    dratcha
    wrote on last edited by
    #1

    I am having a problem with creating a file in c#. More specifically, a problem that happens to the file after creation. First off, here is my code: string strFileName = "c:\Test\file.log"; if(!System.IO.File.Exists(strFileName)) { System.IO.File.Create(strFileName); } The file is created just fine. The probelm is that the aspnet_wp process keeps the file locked after it has been created so I can not write to the file. I can't even open it or delete the file manually. I have to go into Task Manager and kill the process. Is there some sort of cleanup I am missing here? Is it my code or the environment or...??? Thanks.

    C 1 Reply Last reply
    0
    • D dratcha

      I am having a problem with creating a file in c#. More specifically, a problem that happens to the file after creation. First off, here is my code: string strFileName = "c:\Test\file.log"; if(!System.IO.File.Exists(strFileName)) { System.IO.File.Create(strFileName); } The file is created just fine. The probelm is that the aspnet_wp process keeps the file locked after it has been created so I can not write to the file. I can't even open it or delete the file manually. I have to go into Task Manager and kill the process. Is there some sort of cleanup I am missing here? Is it my code or the environment or...??? Thanks.

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      The Create() method (see MSDN[^] for details) returns a FileStream which you are not using. The file will stay locked until the garbage collector removes the FileStream object. Even if you do not want the FileStream object you should at least call Close() (See MSDN[^] for details) on it. Does this help?


      My: Blog | Photos | Next SQL Presentation WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

      D 1 Reply Last reply
      0
      • C Colin Angus Mackay

        The Create() method (see MSDN[^] for details) returns a FileStream which you are not using. The file will stay locked until the garbage collector removes the FileStream object. Even if you do not want the FileStream object you should at least call Close() (See MSDN[^] for details) on it. Does this help?


        My: Blog | Photos | Next SQL Presentation WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

        D Offline
        D Offline
        dratcha
        wrote on last edited by
        #3

        That worked great! Thank you!

        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