June 29, 2010

Open An EMail In Outlook

If are running a PC with Outlook and you want to open an email using Outlook from your application (but not send it immediately)
Following sample opens an email dialog with a specified document already attached
private void butEMailFiles_Click(object sender, RoutedEventArgs e)
{
    string archiveName = GetExistingArchiveName();
    if (File.Exists(archiveName))
    {
        RunShellCommand("Outlook.exe", "/a \"" + archiveName + "\""); 
    }
}

private static void RunShellCommand(string app, string args)
{
    string dir = System.IO.Path.GetDirectoryName(app);
    string file = System.IO.Path.GetFileName(app);

    Process process = new Process();
    process.StartInfo.FileName = file;
    process.StartInfo.WorkingDirectory = dir;
    process.StartInfo.Arguments = args;
    process.Start();
}

Run Batch File in C#

Quick method to create and run a batch file, the contents of which are passed as a string.
private void RunBatchFile(
  string batFileContents, 
  int waitToFinishSecs)
{
    string batFile = Path.GetTempFileName();
    batFile = batFile.Replace(".tmp", ".bat");
    File.WriteAllText(batFile, batFileContents);
    string dir = System.IO.Path.GetDirectoryName(batFile);
    string file = System.IO.Path.GetFileName(batFile);

    Process process = new Process();
    process.StartInfo.FileName = file;
    process.StartInfo.WorkingDirectory = dir;
    bool res = false; // Dont do anything with this yet
    try
    {
        process.Start();
        res = process.WaitForExit(waitToFinishSecs*1000);
        File.Delete(batFile);
    }
    catch (Win32Exception winex)
    {
        Debug.WriteLine(winex.ToString());
        throw;
    }    
}

June 10, 2010

Sweepstake Generator

Highly topical now the world cup is on! Here is my sweepstake generator class
class SweepstakeGenerator
{
  public class PlayerTeamPair
  {
    public string Player { get; set; }
    public string Team { get; set; }
    public override string ToString()
    {
      return (string.IsNullOrEmpty(Team) || string.IsNullOrEmpty(Player)) ? 
        "" : Team + " - " + Player;
    }
  }

  public static IEnumerable AssignTeams(
    IEnumerable orderedTeams, 
    IEnumerable namesArray)
  {
    Debug.Assert(namesArray.Count() > 0, "No names in the names array!");
    List names = new List();
    List teams = new List(orderedTeams);
    List assignedTeams = new List(teams.Count);
    Random random = new Random();
    int teamIx = 0;
    while (teamIx < teams.Count)
    {
      if (names.Count == 0)
      {
        names = names = new List(namesArray);
      }
      int nameIx = random.Next(0, names.Count);
      assignedTeams.Add(new PlayerTeamPair() { 
        Team = teams[teamIx], 
        Player = names[nameIx] });
      //Debug.WriteLine(teams[teamIx] + " - " + names[nameIx]);
      //Console.WriteLine(teams[teamIx] + " - " + names[nameIx]);
      names.RemoveAt(nameIx);
      teamIx++;
    }
    return assignedTeams;
  }
}
and sample usage
string[] teamsOrderedByBettingOdds = new string[] { 
 "Spain", "Brazil", "Argentina", "England",
 "Holland", "Germany", "Italy", "France",
 "Portugal", "Ivory Coast", "Serbia", "Mexico",
 "Chile", "USA", "Paraguay", "Uruguay",
 "Cameroon", "South Africa", "Ghana", "Australia",
 "Denmark", "Nigeria", "Greece", "South Korea",
 "Switzerland", "Slovakia", "Slovenia", "Japan",
 "Algeria", "Honduras", "North Korea", "New Zealand"
 };
string[] teamsOrderedByGrouping = new string[] { 
 "France", "Mexico", "South Africa", "Uruguay",
 "Argentina", "Greece", "Nigeria", "South Korea",
 "Algeria", "England", "Slovenia", "USA",
 "Australia", "Germany", "Ghana", "Serbia",
 "Cameroon", "Denmark", "Japan", "Netherlands",
 "Italy", "New Zealand", "Paraguay", "Slovakia",
 "Brazil", "Ivory Coast", "North Korea", "Portugal",
 "Chile", "Honduras", "Spain", "Switzerland"
 };
string[] teamsOrderedTop8AndThenByGrouping = new string[] { 
 "France", "Argentina", "England", "Germany",
 "Holland", "Italy", "Brazil", "Spain",
 "Mexico", "South Africa", "Uruguay", "Greece",
 "Nigeria", "South Korea", "Algeria", "Slovenia",
 "USA", "Australia", "Ghana", "Serbia",
 "Cameroon", "Denmark", "Japan", "New Zealand",
 "Paraguay", "Slovakia", "Ivory Coast", "North Korea",
 "Portugal", "Chile", "Honduras", "Switzerland" };
 
string[] namesArray = new string[] { 
  "Bob", "Ron", "Tony", "Jim", "George", "Ben", "Alf", "Dirk" };

IEnumerable playerTeamIter = 
  SweepstakeGenerator.AssignTeams(
    teamsOrderedTop8AndThenByGrouping, namesArray);
foreach (SweepstakeGenerator.PlayerTeamPair playerTeam in playerTeamIter)
{
 Debug.WriteLine(playerTeam.ToString());
}
Sample Output
France - George
Argentina - Tony
England - Alf
Germany - Ron
Holland - Bob
Italy - Jim
Brazil - Ben
Spain - Dirk
Mexico - Jim
South Africa - Ben
Uruguay - George
Greece - Dirk
Nigeria - Alf
South Korea - Ron
Algeria - Bob
Slovenia - Tony
USA - Dirk
Australia - Jim
Ghana - Alf
Serbia - George
Cameroon - Tony
Denmark - Ben
Japan - Ron
New Zealand - Bob
Paraguay - Dirk
Slovakia - Alf
Ivory Coast - Jim
North Korea - Ben
Portugal - Ron
Chile - Bob
Honduras - George
Switzerland - Tony