February 16, 2010

ZipStorer

Quick way to include Zip file functionality.
Find up to date version at http://zipstorer.codeplex.com/
The nice thing about ZipStorer is that you dont need to reference any other assemblies, you just include the class file in your project and away you go.
Quick and dirty example of using ZipStorer to compress a bunch of files:
...
saveFilePath = System.IO.Path.ChangeExtension(saveFilePath, "zip");
StringBuilder fileList = new StringBuilder();
string comment = "File changes from: " + 
                 DateTime.UtcNow.ToLongDateString() +
                 Environment.NewLine +
                 "Original path: " + rootDir;                                  
using (ZipStorer zipStore = ZipStorer.Create(saveFilePath, comment))
{
    foreach (CheckedListItem cli in listBoxFilesChanged.SelectedItems)
    {
        if (cli.FileSystemInfo.Exists)
        {
            zipStore.AddFile(ZipStorer.Compression.Deflate, // Compress
               cli.FullName,     // Full path of file to be added
               cli.RelativeName, // Stored as a relative path in zip file
               cli.FullName);    // Comment for stored file (source of the original)
            fileList.AppendLine(cli.FullName);
        }
    }
}
...

Show and ShowDialog in WPF

Show a WPF window in modal form use ShowDialog()
private void ShowXXXDialogModal()
{
    // Instantiate the dialog box
    XXXDlg dlg = new XXXDlg();

    // Configure the dialog box
    dlg.Owner = this;
    // Open the dialog box modally 
    dlg.ShowDialog();
}
Show a WPF window in modaless form (ie dont wait for the opened window to close before returning) use Show()
private void ShowXXXDialogModaless()
{
    // Instantiate the dialog box
    XXXDlg dlg = new XXXDlg();

    // Configure the dialog box
    dlg.Owner = this;
    // Open the dialog box modalessly 
    dlg.Show();
}

February 9, 2010

Dialog To Browse For A Folder

Need these refences
using System.Windows.Forms;
using System.IO;
Sample button logic
  
private void butBrowse_Click(object sender, RoutedEventArgs e)
{
  tbRootDir.Text = BrowseForFolder("Browse to root directory of source", tbRootDir.Text);
}
Browser dialog usage:
private string BrowseForFolder(string descr, string dir)
{
  using (FolderBrowserDialog fbd = new FolderBrowserDialog())
  {
    fbd.Description = descr;
    if (Directory.Exists(dir))
      fbd.SelectedPath = dir;
    if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
      if (Directory.Exists(fbd.SelectedPath))
      {
        dir = fbd.SelectedPath;
      }
    }
  }
  return dir;
}