My Assistant
![]() ![]() |
|
|
Dec 1 2011, 06:41 AM
Post
#1
|
|
|
UtterAccess Editor Posts: 6,716 From: Capital District, NY, USA |
Hi all,
I'm trying to write a standard text log file of some file copy operations. I need to build the string complete during runtime, then output the string to a file (backing up a server, don't want to write the log to disk until all files are processed). I tried using both \n and \r characters when building my string, and my output is sort of correct: when viewed in Notepad, there's no line breaks. When viewed in Notepad++, the line break format is exactly as intended. Not a big deal, I use Notepad++ anyway, but curious why it's not outputting correctly in Notepad? Thanks, ex. notepad.exe CODE TYPE: CORESTART: 20111201_062533END: 20111201_062749ITEM COUNT: 2753ERROR COUNT: 0NO ERRORS ENCOUNTERED\\Server-pesc440\Server\DSServer\DSbe.mdb; OK\\Server-pesc440\Server\DSServer\V4\DSbeCore.mdb; OK\\Server-pesc440\Server\System\Vista\DB\XX\ACDTL.CDX; OK\\Server-pesc440\Server\System\Vista\DB\XX\acdtl.dbf same file in notepad++ CODE TYPE: CORE START: 20111201_062533 END: 20111201_062749 ITEM COUNT: 2753 ERROR COUNT: 0 NO ERRORS ENCOUNTERED \\Server-pesc440\Server\DSServer\DSbe.mdb; OK \\Server-pesc440\Server\DSServer\V4\DSbeCore.mdb; OK \\Server-pesc440\Server\System\Vista\DB\XX\ACDTL.CDX; OK \\Server-pesc440\Server\System\Vista\DB\XX\acdtl.dbf When I copy from Notepad and paste here in UA, the line breaks show. I guess my question is: why isn't notepad displaying the line breaks? Thanks, |
|
|
|
Dec 1 2011, 09:39 AM
Post
#2
|
|
|
UtterAccess Editor Posts: 15,973 From: Northern Virginia, USA |
>> I tried using both \n and \r characters when building my string <<
Have you combined them? strMystring = "Hello\r\nWorld" Or ... have you tried Environment.NewLine? strMystring = "Hello" + Environment.NewLine + "World" |
|
|
|
Dec 1 2011, 10:07 AM
Post
#3
|
|
|
UtterAccess Editor Posts: 6,716 From: Capital District, NY, USA |
I think I tried the combo: \r\n but it did this:
CODE First Line Second Line I'll try Environment.NewLine but I won't see the results until tomorrow. |
|
|
|
Dec 1 2011, 11:12 AM
Post
#4
|
|
|
UtterAccess Editor Posts: 15,973 From: Northern Virginia, USA |
>> I think I tried the combo: \r\n but it did this: <<
Did it show dbl-spacing in Notepad or Notepad++ ? The reason I ask is that Notepad++ is likely to be more "browser-ish" and do well with UNICODE and thus only need \n (or an \r -- can't remember which), where as Notepad is more suited to ASCII encoding (IIRC). How are you creating your text file? There are so many ways and I am wondering whether you are creating your text file as UNICODE text file or an ASCII text file, thus causing the different presentation to the user with different applications. ---- Have you tried to view the file in WordPad? |
|
|
|
Dec 1 2011, 11:52 AM
Post
#5
|
|
|
UtterAccess Editor Posts: 6,716 From: Capital District, NY, USA |
>> Did it show dbl-spacing in Notepad or Notepad++ ? <<
I'd have to try again but I think maybe Notepad++ >> only need \n (or an \r -- can't remember which), << Either one, or both - the view is the same in Notepad++ >> How are you creating your text file? << CODE StreamWriter sr = new StreamWriter("somedir\\somefile.log"); sr.Write(logString); sr.Close >> Have you tried to view the file in WordPad? << I haven't. I'll check that too when I get back into it. For all practical purposes, the log is fine, I don't necessarily need to change it, but the more I know the "why" the better off I'll be later down the road. It might be a bit before I dig back into it fully (on to other projects) but I'll definately keep the suggestions on hand. Thanks! |
|
|
|
Dec 1 2011, 01:35 PM
Post
#6
|
|
|
UtterAccess Editor Posts: 15,973 From: Northern Virginia, USA |
StreamWriter() defaults to UTF-8 encoding (IIRC), but Notepad likes ASCII, and Notepad++ can likely compensate for either encoding. I know your log is not "broken", but you may want to investigate using the File class (which is static) ...
CODE File.WriteAllText("somedir\\somefile.log", logString, Encoding.ASCII); WriteAllText creates/overwrites the file, writes the data, then closes the file. --- If you want to keep your code as it is and still use the StreamWriter, you can still specify the encoding ... CODE StreamWriter sw = new StreamWriter("somedir\\somefile.log", false, Encodeing.ASCII); sw.Write(logString); sw.Close(); Hope this helps! |
|
|
|
Dec 1 2011, 01:44 PM
Post
#7
|
|
|
UtterAccess Editor Posts: 6,716 From: Capital District, NY, USA |
I didn't know we could write with the File class. Looks handy, I'll check it out.
Thanks! |
|
|
|
Dec 1 2011, 01:55 PM
Post
#8
|
|
|
UtterAccess Editor Posts: 15,973 From: Northern Virginia, USA |
|
|
|
|
Dec 2 2011, 06:31 AM
Post
#9
|
|
|
UtterAccess Editor Posts: 6,716 From: Capital District, NY, USA |
Hi Brent,
Looks like Encoding.ASCII did the trick. Of additional note, it's "\r\n" rather than one or the other, according to this statement from the Environment.NewLine MSDN Documentation: QUOTE A string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms. Thanks for the direction - seems to be squared away. |
|
|
|
![]() ![]() |
|
Go to Top · Lo-Fi Version | Time is now: 21st May 2013 - 10:47 PM |