CodeMan
CodeMan

Reputation: 133

The process cannot access the file 'C:\file.txt' because it is being used by another process

I am trying to log each method on my program, I have the application deployed on IIS Server and the user just called me and said the email functionality is not working so I need to basically run the application but log each step into a txt file.

I am declaring the below as a global value:

StreamWriter writer = new StreamWriter("C:\\file.txt");

Then I use it like below in my code:

Method 1
{
  if (file1.HasFile)
 {
 writer.WriteLine("Has File");
}
}

Method 2

private Boolean InsertUpdateData(SqlCommand cmd)
        {
 writer.WriteLine("Insert Started" + DateTime.Now.ToString());
}

So in my case method one runs fine and it writes Has File but when it goes into the second method I get the file is already open which is correct how can I work around this?

Thanks

Global Value - declared at top

namespace WorkOrderManagement
{
    public partial class CreateWorkOrder : System.Web.UI.Page
    {
        bool successfull;
        string path;
        string name;
        string content;
        string datas;
        string ext;
        bool affectedrows;
        string seasonalsupervisor;
        private string sLogFormat;
        private string sErrorTime;

        StreamWriter writer = new StreamWriter("C:\\file.txt");

Upvotes: 1

Views: 12453

Answers (3)

Steve
Steve

Reputation: 216351

I really suggest you to discard the idea to have a global variable to represent a stream and then try to use it in different methods. This is simple in a desktop application, but a lot more complex in an ASP.NET application.

There are simple alternatives that could atomically write your log text and leave the file unlocked.

For example you could have a method like this

public static class Log
{
    public static string _file = "log.txt";
    public static object _locked = new object();

    public static void AppendToLog(string text)
    {
         lock(_locked)
         {
             string path = Server.MapPath("~/APP_DATA");
             File.AppendAllText(Path.Combine(path, _file), text + Environment.NewLine);
         } 
    }
}

Now you can call the log write with

Log.AppendToLog("My message");

I want to underline two important things here. First I don't write in the root drive of the server. This is a bad practice and always a source of problems when you deploy your ASP.NET application in a server where you have not permissions to use anything outside your site root. Thus the ASP.NET system defines a particular folder called APP_DATA under your site root where your application should have read/write permissions. Second point to notice is the use of the lock keyword. This is necessary in an environment like ASP.NET where two users could reach a point of the code where you need to write to the common log file. As MSDN explains it

The lock keyword ensures that one thread does not enter a critical section of code while another thread is in the critical section. If another thread tries to enter a locked code, it will wait, block, until the object is released.

Upvotes: 2

Tapiwa Tachiona
Tapiwa Tachiona

Reputation: 87

you can also do this to close the file stream

using (StreamWriter writer = new StreamWriter("C:\\file.txt"))
{
   //your code here
}
//this automatically closes the stream, and it is more recommended.

Upvotes: 1

Oscar Martinez
Oscar Martinez

Reputation: 621

Close the stream after writing the file

Method 1
{
  if (file1.HasFile)
 {
 writer.WriteLine("Has File");
 writer.Close();
 }
}

Upvotes: 0

Related Questions