IO : simple log to a text file method

private void Log(string text)
{
    File.AppendAllText("log.txt", text);
}

PROCESSES : start a new process

ProcessStartInfo startInfo = new ProcessStartInfo()
{
    FileName = "DOSBox.exe",
    Arguments = "dn1.exe"
};
Process.Start(startInfo);

LINQ : linq - xml - anonymous types

[code]
// load config file
XElement config = XElement.Load("config.xml");
            
// load games
var groups = from gameGroup in config.Descendants("group")
             select new
             {
                 Name = gameGroup.Attribute("name").Value,
                 Games = from game in gameGroup.Descendants("game")
                         select new
                         {
                             Name = game.Attribute("name").Value,
                             Path = game.Attribute("path").Value
                         }
            };
[xml]
<games>
  <group name="arcade">
    <game name="Alley Cat #GGGC" path="\games\alleycat\cat.exe"/>    
    <game name="Dangeours Dave" path="\games\dandave\1.exe"/>
  </group>
  <group name="strategy">
    <game name="Dyna Blaster" path="\games\dynablaster\dyna.exe"/>
  </group>
</games>

IO : replace something in all lines in all the given files

foreach (string file in files)
{
    File.WriteAllLines(file.Replace("_template.cs", ".cs"),
                       File.ReadAllLines(file)
                           .Select(line => string.Format("{0}", line.Replace("$WCREV$", "0"))));
}

EVENTS : simple event

public event EventHandler Error;

...
if (this.Error != null)
    this.Error(this, new EventArgs());
...

UI : how to disable alt+s shortcut

protected override bool ProcessDialogKey(Keys keyData)
{
    if ((Keys.Alt | Keys.S) == keyData)
        return true;
    else
        return base.ProcessDialogKey(keyData);
}

THREADS : simple async task

Task.Factory.StartNew(() =>
{
    Foo();
});

UI : autoscroll

[textbox]
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();

[rich textbox]
richTextBox1s.SelectionStart = richTextBox1.Text.Length; richTextBox1.ScrollToCaret();
[listbox]
listBox1.SelectedIndex = listBox1.Items.Count - 1; listBox1.SelectedIndex = -1;
[listview]
listView1.EnsureVisible(listView1.Items.Count - 1);
[treeview]
treeView1.Nodes[treeView1.Nodes.Count - 1].EnsureVisible();
[datagrid]
dataGridView1.FirstDisplayedCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];

UI : form invoker extension

[code]
static class FormExtensions
{
    static public void UIThread(this Form form, MethodInvoker code)
    {
        if (form.InvokeRequired)
        {
            form.Invoke(code);
            return;
        }
        code.Invoke();
    }
}
[usage]
this.UIThread(() =>
{
    textBox.Text = data;  
});