January 8, 2007

Simple Usage of OpenFileDialog and SaveFileDialog

Insert the following dialog methods into a dialog service class.

OpenFileDialog
public string SelectFileToOpenDialog()
{
  string fileName = string.Empty;

  using (OpenFileDialog of = new OpenFileDialog())
  {
    of.Title = "Open the Hot Fix details file";
    of.Filter = "XXX file (*.xxx)|*.xxx|All files (*.*)|*.*";

    if (of.ShowDialog() == DialogResult.OK)
    {
      fileName =  of.FileName;
    }
  }
  return fileName;
}
SaveFileDialog
private string GetSaveFileName()
{
  string fileName = string.Empty;
  
  // Create new SaveFileDialog object
  using (SaveFileDialog saveFileDlg = new SaveFileDialog())
  {

    saveFileDlg.SupportMultiDottedExtensions = true;

    // Default file extension
    saveFileDlg.DefaultExt = ".zip";

    // Set initial filename (file only NOT directory/path)
    saveFileDlg.FileName = "Changes." + DateTime.UtcNow.ToString("yyyyMMdd");

    // Available file extensions
    saveFileDlg.Filter = "Batch file (*.zip)|*.zip|All files (*.*)|*.*";

    // Adds a extension if the user does not
    saveFileDlg.AddExtension = true;

    // Restores the selected directory, next time
    //saveFileDlg.RestoreDirectory = true;

    // Dialog title
    saveFileDlg.Title = "Where do you want to save the 'zip' file?";

    // Startup directory
    //saveFileDlg.InitialDirectory = @"C:/";

    // Show the dialog and process the result
    if (saveFileDlg.ShowDialog() == DialogResult.OK)
    {
      if (File.Exists(saveFileDlg.FileName))
      {
         File.Delete(saveFileDlg.FileName);
      }
      fileName = saveFileDlg.FileName;
    }
  }
  return fileName;
}
SaveFileDialog Snippet
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Keywords>
        <Keyword>SaveFileDialog</Keyword>
      </Keywords>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Title>SnippetFile1</Title>
      <Author>Roger</Author>
      <Description>SaveFileDialog</Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>SaveFileDialog</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>bat</ID>
          <ToolTip>default file extension</ToolTip>
          <Default>txt</Default>
          <Function>
          </Function>
        </Literal>
      </Declarations>
      <Code Language="csharp"><![CDATA[        
private void SaveFileDialog(string text)
{
  // Create new SaveFileDialog object
  using (SaveFileDialog saveFileDlg = new SaveFileDialog())
  {

    saveFileDlg.SupportMultiDottedExtensions = true;

    // Default file extension
    saveFileDlg.DefaultExt = "$bat$";

    // Available file extensions
    saveFileDlg.Filter = "Batch file (*.$bat$)|*.$bat$|All files (*.*)|*.*";

    // Adds a extension if the user does not
    saveFileDlg.AddExtension = true;

    // Restores the selected directory, next time
    //saveFileDlg.RestoreDirectory = true;

    // Dialog title
    saveFileDlg.Title = "Where do you want to save the 'batch' file?";

    // Startup directory
    //saveFileDlg.InitialDirectory = @"C:/";

    // Show the dialog and process the result
    if (saveFileDlg.ShowDialog() == DialogResult.OK)
    {
      //SaveFileImplement(sf.FileName);
      if (!File.Exists(saveFileDlg.FileName))
      {
        File.WriteAllText(saveFileDlg.FileName, text);
      }
    }
  }
}
  ]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

No comments: