Can I change the value of Environment.NewLine?

I am working with a library that uses Environment.NewLine as a newline char when writing a file. I need him to write files in Unix format, and as such would like to change the new char line.

Can I change the value of Environment.NewLine?

Any other ideas (besides file creation conversion)?

+6
c # newline
source share
7 answers

Can I change the value of Environment.NewLine?

Not. If you know which platform to write to, create classes for each of these platforms and let them have their own implementation. In the simplest case, it would look like this:

public abstract class Platform { public abstract string Newline { get; } } public sealed class Unix : Platform { public override string Newline { get { return "\n"; } } } 

etc., and then just use an instance of the corresponding class.

+10
source share

According to MSDN, the Environment class is sealed, so you cannot inherit it to change the value. As far as I know, this is the only way you could do this. Alternatively, you can try and create a partial class that extends the environment class. I have never tried the latter, so I'm not sure if this will work.

+2
source share

Environment.Newline returns the newline character of the runtime environment in which the .Net runtime is located. You should not use this to convert a newline to another platform newline.

I would recommend implementing your own conversion class.

+1
source share

The Environment.NewLine property is constant, so do not change it. The best thing I could recommend is to change, wherever it is used, to your own constant.

0
source share

Does the third-party library write to the stream or does it pass the file name?

In the first case, you can pass MemoryStream, and then perform a replacement on new lines to get the ones you need. In the latter case, it must be written to a temporary file, and then transferred to the real file (when replacing line endings).

0
source share

God bless, you cannot ... you would completely change the meaning of the System.Environment namespace.

What you want to do is write something for a specific OS (no matter which environment you run the application in), so Environment. this is not what you should use to start.

0
source share

We had this problem when we needed to change the NewLine value to <br /> for web exit.

This required the creation of other property for use in our class. This is actually not the case.

-2
source share

All Articles