...

View Full Version : Resolved C# escape \n\t issue



alykins
11-21-2011, 10:20 PM
I have a streamwriter and I pass a string to it- it does not pick up my escapes though (\n\t) Please see below code nips

the streamwriter section


if (EnableDetailedLog == "1")
{
string LogFileDir = @"C:\";
string LogFilePath = LogFileDir + "DetailedLog" + DateTime.Now.ToString("MMddyyyy") + ".txt";
StreamWriter sw_LogFile = new StreamWriter(LogFilePath, true);
FileStream fs_LogFile = null;

try
{
if (!File.Exists(LogFilePath))
{
fs_LogFile = File.Create(LogFilePath);
sw_LogFile.WriteLine(string.Format("{0} {1}\n", DateTime.Now, DetailedEntry).Trim());
fs_LogFile.Close();
}

else
{
sw_LogFile.WriteLine(string.Format("{0} {1}\n", DateTime.Now, DetailedEntry).Trim());
sw_LogFile.Close();
}

}


and then it get's passed things like this...


str_Contents[0] = string.Format("Last synchronization date was: {0:MM/dd/yyyy}.\n\t Upload failed, Download Failed.", DateTime.Now);
str_Contents[1] = string.Format("Last synchronization date was: {0:MM/dd/yyyy}.\n\t Upload succeeded, Download Failed.", DateTime.Now);
str_Contents[2] = string.Format("Last synchronization date was: {0:MM/dd/yyyy}.\n\t Upload succeeded, Download succeeded.", DateTime.Now);

.........

DetLog.DetailedLogEntry(string.Format("{0} {1}", str_Contents[0], str_Contents[3].Trim()));


instead of newline and then tabbing (\n new line \t horizontal tab) it just "appends" a tiny box [] at the end (now two bracket chars though, an actual square).... does anyone know why it is not picking up my escapes?

oracleguy
11-22-2011, 03:22 PM
What you have looks ok, I'm a little unclear where that second block of code is coming from. Is that in the debug visualizer or in the log file when you look at it does it not have things on different lines?

alykins
11-22-2011, 03:57 PM
sorry for the ambiguity- the first block of code is a snip from a function inside a class I made... so the class looks something like


public class DetailedLogger
{
public void DetailedLog(string args)
{
....... then the stuff I have up there ....
}
}


so what happens then is in my main program I do


DetailedLogger DetLog = new DetailedLogger();


and then I pass it arguments to that fxn within it via


DetLog.DetailedLog(MyString);


but it does not keep the escapes :(

oracleguy
11-28-2011, 03:31 AM
Sorry for the delay in responding. Did you get it figured out? It might be because you are using Unix line ending (\n) and wherever you are displaying this is expecting windows ones (\r\n). Or if you want Environment.NewLine (http://msdn.microsoft.com/en-us/library/system.environment.newline.aspx).

alykins
11-28-2011, 06:38 PM
i have not figured it out yet... i thought about using \r as well thinking that might be the issue... i haven't had a chance to implement it to test though... no worries on delay getting back- i was delayed as well :)- i will post back if the \r works (and i'll check the link u provided as well)

alykins
11-29-2011, 07:41 PM
This is resolved... apparently it was the need for the \r... I got my answer too late to mark as resolved though- if any mod wants to mark it as such would be greatly appreciated :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum