public static void FileWrite(string _log_Code,string _Log_Msg) { string _File_Path = Dir_Path(_log_Code); string _Error_log = string.Empty; if (!File.Exists(_File_Path)) { File.Create(_File_Path); } using (StreamWriter sw = new StreamWriter(@_File_Path, true)) { _Error_log = "***********************************************************************************************************************************************" + Environment.NewLine ; _Error_log += _Log_Msg + Environment.NewLine; _Error_log += "***********************************************************************************************************************************************"; sw.Write(Environment.NewLine + _Error_log); sw.Flush(); sw.Close(); sw.Dispose(); } }
The process cannot access the file 'D:\XXX\XXX\FileStore\Error_Log\Error_Log_07-01-2014.txt' because it is being used by another process.
Quote:The File.Create method creates the file and opens a FileStream on the file. So your file is already open. You don't really need the file.Create method at all: C# string filePath = @"c:\somefilename.txt"; using (StreamWriter sw = new StreamWriter(filePath, true)) { //write to the file } The boolean in the StreamWriter constructor will cause the contents to be appended if the file exists.
string filePath = @"c:\somefilename.txt"; using (StreamWriter sw = new StreamWriter(filePath, true)) { //write to the file }
Stream file = File.Create(_File_Path); StreamWriter sw = new StreamWriter(file);
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)