system.io.ioexception

Posted by Suneel161 under C# on 8/19/2013 | Points: 10 | Views : 1221 | Status : [Member] | Replies : 2
Hi,

the below code getting error "system.io.ioexception the process cannot access the file because it is being used by another process". please can any suggest, how to handle this exception.
string path = context.Current.Server.MapPath("~/UserLogging/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path = path + DateTime.Today.ToString("dd-MMM-yy") + ".log";
if (!File.Exists(path))
{
File.Create(path).Dispose();
}
using (StreamWriter writer = File.AppendText(path))
{
string error = "\r\nLog written at : " + DateTime.Now + " Method : " + userFriendlyError ;
writer.WriteLine(error);
writer.WriteLine("==========================================");
writer.Flush();
writer.Close();
}




Responses

Posted by: Bandi on: 8/19/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer this link
http://www.vbforums.com/showthread.php?625281-RESOLVED-System-IO-IOException-The-process-cannot-access-the-file-txt-because-it-is-being

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Suneel161, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ssj_Kumar on: 8/19/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Suneel try with File.CreateText instead File.Create

Regards,
Jayakumar Selvakani

Suneel161, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response