Sunday, December 11, 2005

Reading ini-file from C#




Here comes small utility class to read settings from old-fashioned-style .ini files
(general sketch only - it reparses file to read each setting):



using System;
using System.IO;
using System.Text.RegularExpressions;

namespace CommonUtilities
{
/// <summary>
/// Parse settings from ini-like files
/// </summary>
public class IniFile
{
static IniFile()
{
_iniKeyValuePatternRegex = new Regex(
@"((\s)*(?<Key>([^\=^\s^\n]+))[\s^\n]*
# key part (surrounding whitespace stripped)
\=
(\s)*(?<Value>([^\n^\s]+(\n){0,1})))
# value part (surrounding whitespace stripped)
"
,
RegexOptions.IgnorePatternWhitespace |
RegexOptions.Compiled |
RegexOptions.CultureInvariant );
}
static private Regex _iniKeyValuePatternRegex;

public IniFile(string iniFileName)
{
_iniFileName = iniFileName;
}

public string ParseFileReadValue(string key)
{
using( StreamReader reader =
new StreamReader(_iniFileName) )
{
do
{
string line = reader.ReadLine();
Match match =
_iniKeyValuePatternRegex.Match(line);
if( match.Success )
{
string currentKey =
match.Groups["Key"].Value as string;
if( currentKey != null &&
currentKey.Trim().CompareTo(key) == 0 )
{
string value =
match.Groups["Value"].Value as string;
return value;
}
}

}
while(reader.Peek() != -1);
}
return null;
}

public string IniFileName
{
get{ return _iniFileName; }
} private string _iniFileName;
}
}





[Listening to: Sax For Lovers - How Many Dreams]



DropMyRights - running applications with restricted rights from your administrative account




Two useful command line utilities (both with source-code) allowing to run process with restricted rights from administrative account: DropMyRights and it's .NET clone DropMyRights.NET.
Very useful to prevent viruses/spyware in your IE, FireFox, MSN Messenger, Outlook etc while running them from your administrative account. Just run them through shortcuts like:
"D:\Program Files\DropMyRights\DropMyRights.exe" "D:\Program Files\FireFox\firefox.exe" C



Thursday, December 08, 2005

G2G Share - service allowing to use your GMail account for file sharing





G2G Share - service allowing to use your GMail account for file sharing. The general idea is that you open your account data for the service and then people may download files from your GMail account without having your login data.

Two more interesting services allowing to upload files to your GMail as if it was flash-disk:
RoamDrive and GMail Drive .




[Listening to: Manu Chao - Infinita Tristeza]


Regular expression to parse .ini files




Regular expression to fetch <key-value> pairs from the old-fashioned .ini files:

((\s)*(?<Key>([^\=^\s^\n]+))[\s^\n]* # key part (surrounding whitespace stripped)
\=
(\s)*(?<Value>([^\n^\s]+(\n){0,1}))) # value part (surrounding whitespace stripped)

Would fetch patterns in form of:



webProxyPort=8080
   webProxyPort  =   8080 
webProxyPort =
8080

Key and Value appears in form of named captures with whitespaces stripped on both ends.