CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2009
    Posts
    90

    How to create a save file dialog in C#

    Hi there

    I have code written that will create a document and insert some content into it but as soon as it has done this, instead of using the Save() or SaveAs() methods that overwrite anything in a predefined location, I want a save file dialog box to appear asking for where the file needs to be saved. How do I do this?

    thanks in advance.

  2. #2
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: How to create a save file dialog in C#

    This sample code does this:
    Code:
                string path="";
                
                FileDialog dlg=new SaveFileDialog();
                DialogResult result = dlg.ShowDialog();
                
                if(result == DialogResult.OK)
                {
                    path=dlg.FileName;
                }
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How to create a save file dialog in C#

    Also make sure to call Dispose() on the dialog, i.e., wrap it in a using block.

  4. #4
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: How to create a save file dialog in C#

    To clarify:
    Code:
     
    using (FileDialog dlg = new SaveFileDialog())
    {
        if (dlg.ShowDialog().Equals(DialogResult.OK)
        {
            String path = dlg.FileName;
    
            File.WriteAllText(path, /* your data string */); // or WriteAllLines/Bytes etc
        }
    }
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: How to create a save file dialog in C#

    Quote Originally Posted by dovobet View Post
    Hi there

    I have code written that will create a document and insert some content into it but as soon as it has done this, instead of using the Save() or SaveAs() methods that overwrite anything in a predefined location, I want a save file dialog box to appear asking for where the file needs to be saved. How do I do this?

    thanks in advance.
    There are samples in MSDN:
    http://msdn.microsoft.com/en-us/libr...iledialog.aspx
    http://msdn.microsoft.com/en-us/library/sfezx97z.aspx
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Dec 2009
    Posts
    90

    Re: How to create a save file dialog in C#

    thank you again, the code works perfectly

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured