Outputting a Platform-Independent EOL Character






Outputting a Platform-Independent EOL Character

Problem

Your application will run on more than one platform. Each platform uses a different end-of-line (EOL) character. You want your code to output the correct EOL character without having to write code to handle the EOL character specially for each platform.

Solution

The .NET Framework provides the Environment.NewLine constant, which represents a newline on the given platform. This is the newline string used by all of the framework provided WriteLine methods internally (including Console, Debug, and trace).

There are a few different scenarios in which this could be useful:

  • Formatting a block of text with newlines embedded within it:

    	// Remember to use Environment.NewLine on every block of text
    	// we format where we want platform-correct newlines inside of.
    	string line;
    	line = String.Format("FirstLine {0} SecondLine {0} ThirdLine {0}",
    	              Environment.NewLine);
    	// Get a temp file to work with.
    	string file = Path.GetTempFileName( );
    	using (FileStream stream = File.Create(file))
    	{
    	    byte[] bytes = Encoding.Unicode.GetBytes(line);
    	    stream.Write(bytes,0,bytes.Length);
    	}
    
    	// Remove the file (good line to set a breakpoint to check out the file
    	// we created).
    	File.Delete(file);
    

  1. You need to use a different newline character than the default one used by StreamWriter (which happens to be Environment.NewLine). You can set the newline that a StreamWriter will use once so that all WriteLines performed by the StreamWriter use that newline instead of having to manually do it each time:

    	// Set up a text writer and tell it to use a certain newline
    	// string.
    	// Get a new temp file.
    	file = Path.GetTempFileName( );
    	line = "Double spaced line";
    	using (StreamWriter streamWriter = new StreamWriter(file))
    	{
    	    // Make this always write out double lines.
    	    streamWriter.NewLine = Environment.NewLine + Environment.NewLine;
    	    // WriteLine on this stream will automatically use the newly specified
    	    // newline sequence (double newline in our case).
    	    streamWriter.WriteLine(line);
    	    streamWriter.WriteLine(line);
    	    streamWriter.WriteLine(line);
    	}
    
    	// Remove the file (good line to set a breakpoint to check out the file
    	// we created).
    	File.Delete(file);
    

  2. Normal WriteLine calls:

    	// Just use any of the normal WriteLine methods as they use the
    	// Environment.NewLine by default.
    	line = "Default line";
    	Console.WriteLine(line);
    

Discussion

Environment.NewLine allows you to have peace of mind, whether the platform is using \n or \r\n as the newline or possibly something else. Your code will be doing things the right way for each platform.

One word of caution here: if you are interoperating with a non-Windows operating system via SOAP and Web Services, the Environment.NewLine defined here might not be accurate for a stream you send to or receive from that other operating system. Of course, if you are doing Web Services, newlines aren't your biggest concern.

See Also

See the "Environment Class" topic in the MSDN documentation.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows