Tuesday, 11 May 2010

Comparing assembly versions in C#

Found a nice little trick to compare version strings in .NET:

Version version1 = new Version("2.4.1");
Version version2 = new Version("2.5");

if (version1 <= version2)
{
// in this case true
}

No need to rely on string.Compare() method !!

Wednesday, 5 May 2010

How to set the default encoding in windows


Go to -> Control Panel - Regional and Language Options; Advanced tab

This impacts the entire computer.

Web links discussing encoding in Windows


The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

Characters Vs. Bytes

How to Use Special Characters in Windows Documents

Windows-1252 (so called ANSI)

(The above links says: ANSI does not necessarily have to map to CP1252. It does, however, always refer to the legacy codepage set for the system. This may be CP1252 on western European or US systems but don't count on that)


Encoding to use while reading/writing files in .NET


"Digging deep into encoding"

While performing File I/O, it is important to keep in mind whether the files being processed can contain special characters or not. If it is so, then check the encoding you are using.

Encoding used in:

StreamReader: it uses UTF-8 encoding by default unless otherwise specified.


StreamWriter: Uses the default encoding of the system unless otherwise specified.

If we do a check on the default encoding - we get "Windows-1252" [which is ANSI]. So while development if we don't specify encoding (and use defaults) then the files created by applications will use ANSI encoding, and applications reading them will use UTF-8!!!

This will give error, and we will see square characters/questions marks in place of the special characters.

Solution:
- We can use encoding "Windows-1252" when reading the file or
- use encoding UTF-8 while writing /reading the files (better approach)

Ironically:
You will get error (if no encoding is specified) only if you are reading an ANSI file using UTF-8 encoding. But you can successfully read a UTF-8 file using the ANSI (Windows-1252) encoding!!!

Tuesday, 4 May 2010

Invalid characters (square boxes) while reading a text file (encoding issue)

Problem:

While reading a text file we get square symbols in .NET for special symbols. For e.g ° .
However if the file is created\saved as Unicode then the file is read correctly.

- tried encoding.UTF-8 but still get the error string
- notepad opens up the file correctly with the special characters

Solution:

Use encoding "Windows-1252".

Sample code -

using (StreamReader reader = new StreamReader(filePath, System.Text.Encoding.GetEncoding("Windows-1252")))
{
string content = reader.ReadToEnd();
reader.Close();
return content;
}

C# Reactive Extensions - Buffer and Window

I was going through Buffer and Window in RX, thought a few examples would help clear the differences. First create a buffer of even numbe...