Regular Expressions for Subversion keywords

Here are different Regular Expressions to identify the common (to me at least), the Subversion keyword: $Id$ on different type of files (depending on how the comments go).

This one is for languages derived from C (like C++, C#, PHP, Java, JavaScript):

// \$Id\: [A-Za-z0-9\ \:\.\-\_]* \$

It will match something like:

// $Id: filename [...]

Remove duplicated whitespaces on C#

The Trim method on C# only removes whitespaces at the beginning and at the end of a string, but it does not remote duplicated whitespaces inside the string (like the Trim function on VB for Apps), which is very handy sometimes.

One of the ways to do it on C#, is using Regular Expressions. This [...]

Convert a String to a Stream

In C#, to convert a String into a Stream object, we need to use the GetBytes method (from the Encoding.ASCII package), this way:

byte[] bytes = Encoding.ASCII.GetBytes(xmlContent);

And then, use that byte array when instantiate a Stream (for example, MemoryStream or FileStream):

MemoryStream stream = new MemoryStream(bytes);

Convertir un IList en un Array

Existen algunas formas de hacerlo, acá les dejo un par.

Primera forma:

//list es del tipo IList ArrayList arr = new ArrayList(list); Item[] items = (Item[])arr.ToArray(typeof(Item));

Otra forma:

Item[] items = new Item[list.Count]; list.CopyTo(items, 0);

Convertir un ArrayList a string[]

Para convertir en C# un objecto ArrayList, definido como:

ArrayList arrayList = new ArrayList();

en un arreglo de strings (string[]), solo se necesita:

string[] stringArray = (string[])arrayList.ToArray(typeof(string));

Diferencia entre ICollection e IList en C#

Los elementos dentro de un ICollection (para ser más exactos, los elementos dentro de una clase que implemente una interfaz ICollection) no tienen un orden específico. En un momento pueden estar ordenados de una manera, y en otro, sin alguna razón aparente, ordenados de otra forma.

En cambio, los elementos de un IList si tienen [...]

Obtener el MimeType de un archivo desde C#

No he encontrado una manera directa de hacerlo, pero encontré esta función que busca la extensión del archivo en el registro de windows y si está registrada, obtiene su tipo mime.

private string getMimeType(string fileName) { string mimeType = "application/unknown"; string ext = System.IO.Path.GetExtension(fileName).ToLower(); Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext); if (regKey != null && regKey.GetValue("Content Type") [...]