I have tried to write a string on text file,but its not writing anything and there is no exceptions. My code is:
public void CreateLog(string sLogInfo)
{
string sDestionation = null;
string sFileName = DateTime.Now.ToString("yyyyMMdd") + "_log.txt";
sDestionation = #"D:\Log\";
//sDestionation = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + ConfigurationManager.AppSettings["DestinationPath"].ToString();
string sFile = sDestionation + sFileName;
if (!System.IO.Directory.Exists(sDestionation))
{
System.IO.Directory.CreateDirectory(sDestionation);
}
StreamWriter oWriter = null;
if (!System.IO.File.Exists(sFile))
{
oWriter = File.CreateText(sFile);
}
else
{
oWriter = File.AppendText(sFile);
}
oWriter.WriteLine(DateTime.Now.ToString() + ": " + sLogInfo.Trim());
}
StreamWriter is IDisposable object. You should dispose it after using. For this you can use using statement like this:
public void CreateLog(string sLogInfo)
{
string sDestionation = null;
string sFileName = DateTime.Now.ToString("yyyyMMdd") + "_log.txt";
sDestionation = #"D:\Log\";
var sFile = sDestionation + sFileName;
if (!Directory.Exists(sDestionation))
{
Directory.CreateDirectory(sDestionation);
}
using (var oWriter = new StreamWriter(sFile, true))
oWriter.WriteLine(DateTime.Now + ": " + sLogInfo.Trim());
}
Use File.AppendAllText that will do all the steps (except creating folder) for you.
Otherwise you should properly dispose writer when you are done, preferably with using in the same function:
using(oWriter)
{
oWriter.WriteLine(DateTime.Now.ToString() + ": " + sLogInfo.Trim());
}
Your code looks fine, however, I think you should add at the end of it the following:
oWriter.Close()
You should flush (disposing is enough) your data into the file at the end of your code:
oWriter.Flush(); //Save (Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream.)
oWriter.Dispose(); //Then free this resource
As Yuval mentioned looking at C#'s StreamWriter.cs class it does indeed calls the Flush method internally. See here: Reference
Related
I have a problem that I know people already asked here but I tried the solution they bring but it's not helping.
My problem: I'm doing a program in C# with 2 forms. My main form is used to read a file .txt and put the information in a DataGridView:
public void LireFichier()
{
DataGridView dataGridView1 = new DataGridView();
string delimeter = ";";
string tableName = "Clients";
string filePath = #"C:...\Clients.txt";
DataSet dataset = new DataSet();
using (StreamReader sr = new StreamReader(filePath))
{
dataset.Tables.Add(tableName);
dataset.Tables[tableName].Columns.Add("ID");
dataset.Tables[tableName].Columns.Add("Name");
dataset.Tables[tableName].Columns.Add("LastName");
dataset.Tables[tableName].Columns.Add("Datet");
dataset.Tables[tableName].Columns.Add("Price");
dataset.Tables[tableName].Columns.Add("Phone");
dataset.Tables[tableName].Columns.Add("ID box");
string allData = sr.ReadToEnd();
string[] rows = allData.Split("\r".ToCharArray());
foreach (string r in rows)
{
string[] items = r.Split(delimeter.ToCharArray());
dataset.Tables[tableName].Rows.Add(items);
}
this.dataGridView1.DataSource = dataset.Tables[0].DefaultView;
}
My second form is used to add a client in my .txt file :
private void btn_Confirmer_Click(object sender, EventArgs e)
{
string filePath = #"...\Clients.txt";
Client client = new Client();
client.Id = idClient++;
client.Name = tb_name.Text;
client.LastName = tb_lastName.Text;
client.Date = dtp_date.Value.ToString("dd-MMM-yyyy");
client.Price = Convert.ToInt32(tb_price.Text);
client.Phone = tb_telephone.Text;
client.ID_Box = Convert.ToInt32(tb_idbox.Text);
string clientInfo = client.Id.ToString() + ";" + client.Name.ToString() + ";" + client.LastName.ToString() + ";" + client.Date.ToString() + ";" +
client.Montant.ToString() + ";" + client.Phone.ToString() + ";" + client.ID_Boc.ToString() + ";";
using (StreamReader sr = new StreamReader(filePath))
{
string allData = sr.ReadToEnd() + clientInfo;
File.WriteAllText(filePath, allData);
}
this.Close();
}
The problem remains in the StreamReader or the File.WriteAllText no matter what I do I always encounter the same exception(System.IO.IOException) that my file is in use when I arrived to write(add a client) in the file in my second form.
The solution that I tried are:
put the using blocks
sr.Close()
sr.Dispose() even if I know that the using block call dispose at the end.
sr.Close() and sr.Dispose() at the end of the instruction.
Try changing to the following since I think the write is interfering with the read you are already performing.
string allData;
using (StreamReader sr = new StreamReader(filePath))
{
allData = sr.ReadToEnd() + clientInfo;
}
this.Close();
File.WriteAllText(filePath, allData);
Trelly indicated the most possible cause of you problem (file already in use). However, the solution containing exception handling should look like this:
try
{
string allData = null;
using (var sr = new StreamReader(filePath))
{
allData = sr.ReadToEnd() + clientInfo;
}
File.WriteAllText(filePath, allData);
}
catch (IOException exc)
{
// exception handling code
}
this.Close();
OR
It looks like you want to just append some text at the end of the file. File.AppendText is your friend:
using (StreamWriter sw = File.AppendText(filePath))
{
sw.Write(clientInfo);
}
To make your blocks of code thread safe, I would use a lock statement.
First declare object, you will perform lock on:
public static readonly Object Locker = new Object();
Then surround your parts of code, where you are working with file with lock statement:
lock(Locker)
{
//perform file operations
}
Remember that both forms have to refer to the same Locker object
After ReadToEnd, close the StreamReader before WriteAllText:
using (StreamReader sr = new StreamReader(filePath))
{
string allData = sr.ReadToEnd() + clientInfo;
sr.Close() ; // <---- add this instruction
File.WriteAllText(filePath, allData);
}
This code will create "output.txt" in C:\temp if it doesn't already exist. However, log.WriteLine(); doesn't work for me. When I open the file, I don't see it. Why is this?
private static string LogFile = #"C:\Temp\output.txt";
private StreamWriter log;
if (!File.Exists(#"" + LogFile))
{
log = new StreamWriter(#"" + LogFile);
}
else {
log = File.AppendText(#"" + LogFile);
}
log.WriteLine("["+DateTime.Now + "]: ");
You need to close the StreamWriter. It's best to use a using block for this, as it guarantees that the StreamWriter will be closed, even if an exception is thrown.
using (var log = GetLog()){
log.WriteLine("["+DateTime.Now + "]: ");
}
...
public StreamWriter GetLog(){
if (!File.Exists(#"" + LogFile))
{
return new StreamWriter(#"" + LogFile);
}
else {
return File.AppendText(#"" + LogFile);
}
}
Just a little improvement in code to #John Saunders answer.
using (var log = GetLog())
{
log.WriteLine("["+DateTime.Now + "]: ");
}
...
public StreamWriter GetLog()
{
return new StreamWriter(LogFile, File.Exists(LogFile));
}
The second parameter StreamWriter constructer takes determines append operation. Thus if file exists append otherwise not will do it. I think this is neater. And actually you can even do:
using (var log = new StreamWriter(LogFile, true))
{
log.WriteLine("["+DateTime.Now + "]: ");
}
Always appends, and if file does not exist creates it.
If you are always appending the same thing to the file whether or not it already exists, you don't need the conditional.
File.AppendText will create the file if it doesn't exist.
private static string LogFile = #"C:\Temp\output.txt";
using (StreamWriter sw = File.AppendText(LogFile))
{
sw.WriteLine("["+DateTime.Now + "]: ");
}
As John Saunders already said, you need to Dispose the stream writer, which will be done by putting it in a using statement.
(Documentation for StreamWriter)
Ok so heres the thing. When ever a trigger is hit i append my logs in a specific folder. The code works fine and it appends it correctly but if i try to manually delete the folder from the desktop its giving a "The action cannot be completed cause the folder/file is open in another program";
i guess im not disposing it right but i dont know where i missed it. I know its the folder that is attached to the process cause i tried to delete the .log file inside and it allowed me.
private void LogEvent(string filename,bool AppendTxt,string msg)
{
string sLogFormat = DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.ToLongTimeString().ToString() + " ==> ";
msg = sLogFormat + msg;
// create directory
if (System.IO.Directory.Exists("C:\\Users\\DT-Npax\\Desktop\\LOGS1") != true)
{
Directory.CreateDirectory("C:\\Users\\DT-Npax\\Desktop\\LOGS1");
}
string dailyLog = "C:\\Users\\DT-Npax\\Desktop\\LOGS1" + "\\" + filename + ".log";
FileStream FS = null;
//write or append txt
if (!AppendTxt)
{
if (File.Exists(dailyLog))
{
File.Delete(dailyLog);
}
using (FS = File.Create(dailyLog)) { }
FS.Close();
StreamWriter TXT_WRITE = new StreamWriter(dailyLog);
TXT_WRITE.WriteLine(msg);
TXT_WRITE.Close();
}
else
{
if (!File.Exists(dailyLog))
{
using (FS = File.Create(dailyLog)) { }
FS.Close();
}
FileStream FSAppend = new FileStream(dailyLog, FileMode.Append, FileAccess.Write);
StreamWriter TXT_WRITE = new StreamWriter(FSAppend);
TXT_WRITE.WriteLine(msg);
TXT_WRITE.Close();
FSAppend.Close();
}
}
Your code does seem to close the file properly but not in an exception-safe manner.
You also have some unnecessary code in there (like using (FS = File.Create(dailyLog)) { } FS.Close(); ).
The smallest modification looks like this:
else
{
//if (!File.Exists(dailyLog))
//{
// using (FS = File.Create(dailyLog)) { }
// FS.Close();
//}
using (FileStream FSAppend = new FileStream(dailyLog, FileMode.Append, FileAccess.Write))
using (StreamWriter TXT_WRITE = new StreamWriter(FSAppend))
{
TXT_WRITE.WriteLine(msg);
}
//TXT_WRITE.Close();
//FSAppend.Close();
}
But I would rewrite this whole method like:
private void LogEvent(string filename,bool AppendTxt,string msg)
{
string sLogFormat = DateTime.Now.ToShortDateString().ToString() + " "
+ DateTime.Now.ToLongTimeString().ToString() + " ==> ";
msg = sLogFormat + msg;
// create directory
if (System.IO.Directory.Exists("C:\\Users\\DT-Npax\\Desktop\\LOGS1") != true)
{
Directory.CreateDirectory("C:\\Users\\DT-Npax\\Desktop\\LOGS1");
}
string dailyLog = "C:\\Users\\DT-Npax\\Desktop\\LOGS1" + "\\" + filename + ".log";
if (AppendText)
System.IO.File.AppendAllText(dailylog, msg);
else
System.IO.File.WriteAllText(dailylog, msg);
}
There is no need to pre-create or delete files.
Wrap the streams in a using block since they implement IDisposable.
I must say this code is a little odd...
using (FS = File.Create(dailyLog)) { }
FS.Close();
StreamWriter TXT_WRITE = new StreamWriter(dailyLog);
TXT_WRITE.WriteLine(msg);
TXT_WRITE.Close();
Shouldn't it be something like:
using (FileStream FS = File.Create(dailyLog))
{
using(StreamWriter TXT_WRITE = new StreamWriter(dailyLog))
{
TXT_WRITE.WriteLine(msg);
}
}
I have a question that's driving me nuts. I have a program that saves error messages to a string in an object, then writes the string to a file in the unloadContent() thing. For some reason I keep getting Not Supported Exceptions. Here is the code in unloadContent():
if (debug.getContent().Length > 0)
{
saveErrors save = new saveErrors();
if (Directory.Exists(System.IO.Directory.GetCurrentDirectory() + "\\Errors")) ;
Directory.CreateDirectory(System.IO.Directory.GetCurrentDirectory() + "\\Errors");
save.save(System.IO.Directory.GetCurrentDirectory().ToString() + "\\Errors\\errorLog_" + (System.DateTime.Now.ToString().Replace("/", "_")).Replace(" ","") + ".txt");
}
and here's the code in class save errors:
public class saveErrors
{
private string mess = debug.getContent();
public void save(string fileName)
{
Debug.WriteLine(fileName);
using (StreamWriter sw = new StreamWriter(fileName))
{
sw.Write(mess);
sw.Close();
}
}
}
I'm still a bit new to C#, so any help would be greatly appreciated!
Thanks!
Try this:
[Test]
public void SaveTextTest()
{
string relativePath=#"Errors\errorLog_";
string directoryPath = System.IO.Path.Combine( System.IO.Directory.GetCurrentDirectory() , relativePath);
var directoryInfo = new DirectoryInfo(directoryPath);
if(directoryInfo.Exists==false)
directoryInfo.Create();
string fileName = System.DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss") + ".txt";
string path = System.IO.Path.Combine(directoryPath, fileName);
string textToSave = "This will be saved";
File.WriteAllText(path, textToSave);
}
To get the DateTime.ToString() in the desired format you can pass a formatstring
save.save(System.IO.Directory.GetCurrentDirectory().ToString() + "\\Errors\\errorLog_" + (System.DateTime.Now.ToString().Replace("/", "_")).Replace(" ", "").Replace(":", "") + ".txt");
Change it to that. You need a .Replace(":", "") because : Is included in the date part of the code, but is invalid in a file name, so you must either remove it or replace it with something else.
As an alternative you could format the date as so:
save.save(System.IO.Directory.GetCurrentDirectory().ToString() + "\\Errors\\errorLog_" + System.DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss"));
my first problem comes in the form of if i declare my filestream etc in this manner
filestream file;
streamreader file_in;
streamwriter file_out;
try
{
file = new filestream("data.txt", FileMode.OpenOrCreate);
file_in = new streamreader(file);
file_out = new streamwriter(file);
}
catch(IOException exc)
{
Console.WriteLine(exc.Message);
}
throws an error which says "use of unassigned local variable", which i find odd because all streams are declared outside of the try block but within the main so they should exist within the main.
my other problem comes in the form that if i remove the try/catch block and just declare the streams as one line (eg: FileStream file = new FileStream("data.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);) my reading from file does work however i cannot write to file. my write to file function is as follows:
public bool write_to_file(ref StreamWriter file_out)
{
if (this.is_empty == true)
{
Console.WriteLine("error, there is nothing to write.");
Console.WriteLine("press any key to continue...");
Console.ReadKey();
return false;
}
try
{
string temp = this.is_empty + "," + this.movie_title + "," + this.year_released + "," + this.publisher + "," +
this.length + "," + this.acting_rating + "," + this.music_rating + "," + this.cinematography_rating + "," +
this.plot_rating + "," + this.duration_rating + "," + this.total_rating;
file_out.WriteLine(temp);
return true;
}
catch (IOException exc)
{
Console.WriteLine(exc.Message);
Console.WriteLine("press any key to continue...");
Console.ReadKey();
return false;
}
}
any help would be much appreciated, thanks.
Well, they're declared but unassigned... so, either set them to null or just do everything together.
try
{
using(var file = new FileStream("data.txt", FileMode.OpenOrCreate))
using(var file_in = new StreamReader(file))
using(var file_out = new StreamWriter(file))
{
// Do your thing
}
}
catch
{
throw;
}
You need to assign a value to your variables at the top, even if its just null
FileStream file = null;
StreamReader file_in = null;
StreamWriter file_out = null;
Before closing the files, try flushing the output streams and files.
file_out.Flush();
file.Flush(); // may be redundant but won't hurt