c# - How do I solve the error "The Process Cannot access the file because it is being used by another process" -


i trying create log file using code given below, raising following error: -

the process cannot access file because being used process

kindly me out in regard.

the code given below: -

string logfile = system.io.directory.getcurrentdirectory() + "\\logfile\\" + datetime.now.tostring("dd-mm-yyyy") + "tasklogfile.txt"; streamwriter logwriter = null;  try {     if (!file.exists(logfile))     {               logwriter = new streamwriter(logfile);         console.writeline("log file created...");     }     else     {         logwriter = file.appendtext(logfile);         console.writeline("log file appended...");     }      if (ex == null)     {         logwriter.write("\r\nlog entry : ");         logwriter.writeline("{0} {1}", datetime.now.tolongtimestring(), datetime.now.tolongdatestring());         logwriter.writeline("\ntask id = {0} , task name = {1}, service name = {2} ", taskid, taskname, servicename);         logwriter.writeline("-----------------------------------------------------------------------------------------------------");     }      else     {         logwriter.writeline("\n error : {0}, error location : {1},task id = {2}, task name : {3}, service name : {4} " + ex.tostring(), ex.stacktrace, taskid, taskname, servicename);     }  }  catch (exception ex) {     logwriter = file.appendtext(logfile);     logwriter.writeline("\n error : {0}" + ex.tostring()); }   {     if (logwriter != null)     {         logwriter.close();          }     logwriter = null; } 

update catch(){} block , try;

catch (exception ex)     {          if (logwriter != null)         {             logwriter.close();              }         logwriter = file.appendtext(logfile);         logwriter.writeline("\n error : {0}" + ex.tostring());     } 

hope helps..!


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -