Newly created file stays locked
-
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. -
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.The
Create()
method (see MSDN[^] for details) returns aFileStream
which you are not using. The file will stay locked until the garbage collector removes theFileStream
object. Even if you do not want theFileStream
object you should at least callClose()
(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
-
The
Create()
method (see MSDN[^] for details) returns aFileStream
which you are not using. The file will stay locked until the garbage collector removes theFileStream
object. Even if you do not want theFileStream
object you should at least callClose()
(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