Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to use saveFileDialog

how to use saveFileDialog

Scheduled Pinned Locked Moved C#
csharphelptutorial
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Jamie Nordmeyer

    SaveFileDialog dlg = new SaveFileDialog();
    dlg.Filter = "Text Files|*.txt|Comma Seperated Files|*.csv|All Files|*.*";
    dlg.InitialDirectory = @"C:\";
    if (dlg.ShowDialog() == DialogResult.OK)
    {
    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(dlg.FileName))
    {
    writer.Write(txtContent.Text);
    writer.Flush();
    }
    }

    The Filter property is a pipe (vertical bar) separated list of file type descriptions and file type filters that must be paired. So in the above example, when the Save File Dialog is shown, the 'File Types' drop down will contain 3 entries: one for Text Files, which will expect a .txt extension, one for Comma Seperated Values, which will expect a .csv extension, and one for All Files, which will take any extension. The OpenFileDialog class works in the same way.

    Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA

    B Offline
    B Offline
    Blekk
    wrote on last edited by
    #3

    Thanks, this has really helped me, codeproject is awesome :-D

    J N 2 Replies Last reply
    0
    • B Blekk

      Thanks, this has really helped me, codeproject is awesome :-D

      J Offline
      J Offline
      Jamie Nordmeyer
      wrote on last edited by
      #4

      No problem. And yes it is! :-D

      Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA

      B 2 Replies Last reply
      0
      • J Jamie Nordmeyer

        No problem. And yes it is! :-D

        Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA

        B Offline
        B Offline
        Blekk
        wrote on last edited by
        #5

        How do you learn stuff like that, have you got books? If so, can you recommend any good ones? Thanks.

        J 1 Reply Last reply
        0
        • J Jamie Nordmeyer

          No problem. And yes it is! :-D

          Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA

          B Offline
          B Offline
          Blekk
          wrote on last edited by
          #6

          How do you work out that kind of thing? Have you got books? If so, can you recommend any? Thanks. (sorry if this has double posted, something up with my connection)

          1 Reply Last reply
          0
          • B Blekk

            How do you learn stuff like that, have you got books? If so, can you recommend any good ones? Thanks.

            J Offline
            J Offline
            Jamie Nordmeyer
            wrote on last edited by
            #7

            Part of it is from books, part of it is from shear experience in the work force. I've been writing software, both Windows and Web for quite a while now, and these are just the sort of things you tend to pick up. Your best bet for learning the framework stuff is to pick an applicaiton, and make your own version of it, simply for the learning experience of how to do it. A notepad clone, with a multi-document interface, toolbars, a status bar, menus, context menus, file opening and saving, and any other features you can think of. Start with small pieces, and work your way up. You can also jump over to Amazon.com and do a search for Beginning C#. There's a ton of books available, some better than others, so make sure to read the buyer comments before making a purchase. Also, this site here truly is a wonderful resource, and if you're new to it, you should become as familiar with it as possible. If you can't find what you're looking for in an article, then by all means, post a question in the forum. Just make sure it's the correct forum if you don't want to get flamed. ;) Folks here may not always have the correct answer, but chances are that someone can atleast point you in the right direction.

            Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA

            B 1 Reply Last reply
            0
            • B Blekk

              Thanks, this has really helped me, codeproject is awesome :-D

              N Offline
              N Offline
              Nadia Monalisa
              wrote on last edited by
              #8

              You can also follow the simpler code like this if you are using .NET 2.0 (VS 2005) SaveFileDialog dlg = new SaveFileDialog(); dlg.Filter = "Text Files|*.txt|Comma Seperated Files|*.csv|All Files|*.*"; if (dlg.ShowDialog() == DialogResult.OK) { File.WriteAllText(dlg.FileName, txtContent.Text); }

              J 1 Reply Last reply
              0
              • J Jamie Nordmeyer

                Part of it is from books, part of it is from shear experience in the work force. I've been writing software, both Windows and Web for quite a while now, and these are just the sort of things you tend to pick up. Your best bet for learning the framework stuff is to pick an applicaiton, and make your own version of it, simply for the learning experience of how to do it. A notepad clone, with a multi-document interface, toolbars, a status bar, menus, context menus, file opening and saving, and any other features you can think of. Start with small pieces, and work your way up. You can also jump over to Amazon.com and do a search for Beginning C#. There's a ton of books available, some better than others, so make sure to read the buyer comments before making a purchase. Also, this site here truly is a wonderful resource, and if you're new to it, you should become as familiar with it as possible. If you can't find what you're looking for in an article, then by all means, post a question in the forum. Just make sure it's the correct forum if you don't want to get flamed. ;) Folks here may not always have the correct answer, but chances are that someone can atleast point you in the right direction.

                Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA

                B Offline
                B Offline
                Blekk
                wrote on last edited by
                #9

                Lol ok, thanks mate. A text editor is actually what I am trying to create. Somewhere else in the forum a guy said he needed to make a text editor for a project at a training place or something, so I wanted to see if I was up to it. Thanks for the Save part of it, I'll just try and work out the Open and other stuff part. Im sure it will be pretty similar. Thanks.

                J 1 Reply Last reply
                0
                • B Blekk

                  Lol ok, thanks mate. A text editor is actually what I am trying to create. Somewhere else in the forum a guy said he needed to make a text editor for a project at a training place or something, so I wanted to see if I was up to it. Thanks for the Save part of it, I'll just try and work out the Open and other stuff part. Im sure it will be pretty similar. Thanks.

                  J Offline
                  J Offline
                  Jamie Nordmeyer
                  wrote on last edited by
                  #10

                  Yeah, very similar. The difference is more how it's used than in how it's coded. And again, don't be afraid to ask questions. As long as the questions are like "How do you use the SaveFileDialog control?", and not "Can someone give me the sourcecode for a FTP Program", you'll do fine.

                  Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA

                  1 Reply Last reply
                  0
                  • N Nadia Monalisa

                    You can also follow the simpler code like this if you are using .NET 2.0 (VS 2005) SaveFileDialog dlg = new SaveFileDialog(); dlg.Filter = "Text Files|*.txt|Comma Seperated Files|*.csv|All Files|*.*"; if (dlg.ShowDialog() == DialogResult.OK) { File.WriteAllText(dlg.FileName, txtContent.Text); }

                    J Offline
                    J Offline
                    Jamie Nordmeyer
                    wrote on last edited by
                    #11

                    Hmm. I've never called this method before. It must call Flush internally. I've had mixed results closing or disposing the StreamWriter class without flushing first. Thanks for the tip. :)

                    Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA

                    1 Reply Last reply
                    0
                    • J Jamie Nordmeyer

                      SaveFileDialog dlg = new SaveFileDialog();
                      dlg.Filter = "Text Files|*.txt|Comma Seperated Files|*.csv|All Files|*.*";
                      dlg.InitialDirectory = @"C:\";
                      if (dlg.ShowDialog() == DialogResult.OK)
                      {
                      using (System.IO.StreamWriter writer = new System.IO.StreamWriter(dlg.FileName))
                      {
                      writer.Write(txtContent.Text);
                      writer.Flush();
                      }
                      }

                      The Filter property is a pipe (vertical bar) separated list of file type descriptions and file type filters that must be paired. So in the above example, when the Save File Dialog is shown, the 'File Types' drop down will contain 3 entries: one for Text Files, which will expect a .txt extension, one for Comma Seperated Values, which will expect a .csv extension, and one for All Files, which will take any extension. The OpenFileDialog class works in the same way.

                      Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA

                      V Offline
                      V Offline
                      veluru krishna
                      wrote on last edited by
                      #12

                      hi, when use the code given by you it saves a file but unfortunately it saves it without newline characters ie.,. I want to save my work as it is displayed in the multiline text box.can you help me please

                      Don't wait to hear a word of thanks from anybody when you help them instead ask them to help three other people and ask them to continue in chain.

                      J 1 Reply Last reply
                      0
                      • V veluru krishna

                        hi, when use the code given by you it saves a file but unfortunately it saves it without newline characters ie.,. I want to save my work as it is displayed in the multiline text box.can you help me please

                        Don't wait to hear a word of thanks from anybody when you help them instead ask them to help three other people and ask them to continue in chain.

                        J Offline
                        J Offline
                        Jamie Nordmeyer
                        wrote on last edited by
                        #13

                        Are you writing each line on its own using the StreamWriter.Write method, or are you writing the entire contents of the textbox to the Write method? If you're writing each line on its own, then you need to use WriteLine, not Write.

                        Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups