using System.IO;
using System.Windows.Forms;
namespace Maestro.Shared.UI
{
///
/// Helper class to overcome some of the bad default values assigned to the and
///
/// It is preferable to use this class instead of creating the or yourself.
///
public static class DialogFactory
{
private static string _lastOpenDir;
private static string _lastSaveDir;
///
/// Creates an with sensible default values
///
///
public static OpenFileDialog OpenFile()
{
var dlg = new OpenFileDialog();
// I guess someone at MSFT must've went over the wrong side of the ballmer peak
// because RestoreDirectory should not default to false. Defaulting to false
// means that attempting to load files/assemblies using relative paths
// will most likely fail because for some reason, using this dialog will
// actually modify the current working directory!
//
// So we set this to true, but to preserve the existing behaviour, we store
// the directory of the selected file in a static variable on dialog close (OK),
// and assign this dir to the InitialDirectory property on each dialog request,
// effectively replicating the old behaviour (w/o the nasty side effects)
//
// Or maybe my usage scenario does not qualify as a common use case.
dlg.RestoreDirectory = true;
if (Directory.Exists(_lastOpenDir))
{
dlg.InitialDirectory = _lastOpenDir;
}
dlg.FileOk += (sender, e) =>
{
_lastOpenDir = Path.GetDirectoryName(dlg.FileName);
};
return dlg;
}
///
/// Creates a with sensible default values
///
///
public static SaveFileDialog SaveFile()
{
var dlg = new SaveFileDialog();
// I guess someone at MSFT must've went over the wrong side of the ballmer peak
// because RestoreDirectory should not default to false. Defaulting to false
// means that attempting to load files/assemblies using relative paths
// will most likely fail because for some reason, using this dialog will
// actually modify the current working directory!
//
// So we set this to true, but to preserve the existing behaviour, we store
// the directory of the selected file in a static variable on dialog close (OK),
// and assign this dir to the InitialDirectory property on each dialog request,
// effectively replicating the old behaviour (w/o the nasty side effects)
//
// Or maybe my usage scenario does not qualify as a common use case.
dlg.RestoreDirectory = true;
if (Directory.Exists(_lastSaveDir))
{
dlg.InitialDirectory = _lastSaveDir;
}
dlg.FileOk += (sender, e) =>
{
_lastSaveDir = Path.GetDirectoryName(dlg.FileName);
};
return dlg;
}
}
}