January 8, 2007

Simple Usage of OpenFileDialog and SaveFileDialog

OpenFileDialog
using (OpenFileDialog of = new OpenFileDialog())
{
  of.Title = "Open the XXX file";
  of.Filter = "XXX files (*.xxx)|*.xxx";
  // Optional: Set the initial directory to look in
  // if (Directory.Exists(someDir))
  //   of.InitialDirectory = someDir;
  // Optional: Set the initial filename
  // of.FileName = "???.exe";
  if (of.ShowDialog() == DialogResult.OK)
  {
    if (File.Exists(of.FileName))
    {
      DoReadFile(of.FileName); 
      // eg string someString = File.ReadAllText(of.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: