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. open filestream

open filestream

Scheduled Pinned Locked Moved C#
questiondiscussion
7 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.
  • E Offline
    E Offline
    electriac
    wrote on last edited by
    #1

    I got this piece of code from, I know not where, as a recommended way to write to a file.

    // better way to write to file stream
    using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
    {
    StreamWriter sw = new StreamWriter(fs);
    sw.Write(textBox1.Text);
    sw.Close();
    }

    Any thoughts?

    L Richard DeemingR 3 Replies Last reply
    0
    • E electriac

      I got this piece of code from, I know not where, as a recommended way to write to a file.

      // better way to write to file stream
      using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
      {
      StreamWriter sw = new StreamWriter(fs);
      sw.Write(textBox1.Text);
      sw.Close();
      }

      Any thoughts?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      electriac wrote:

      Any thoughts?

      My first thought is, "what is your question"?

      Veni, vidi, abiit domum

      E 1 Reply Last reply
      0
      • L Lost User

        electriac wrote:

        Any thoughts?

        My first thought is, "what is your question"?

        Veni, vidi, abiit domum

        E Offline
        E Offline
        electriac
        wrote on last edited by
        #3

        Does this appear to be correct?

        L D 2 Replies Last reply
        0
        • E electriac

          Does this appear to be correct?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Appearances can be deceiving. Why not test it for yourself to see what happens, you may be surprised at what you can learn that way.

          Veni, vidi, abiit domum

          1 Reply Last reply
          0
          • E electriac

            Does this appear to be correct?

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            The term "correct" would be dependent upon the codes overall purpose. Since we know nothing of what you're doing or why, it's impossible to answer the question.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

            1 Reply Last reply
            0
            • E electriac

              I got this piece of code from, I know not where, as a recommended way to write to a file.

              // better way to write to file stream
              using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
              {
              StreamWriter sw = new StreamWriter(fs);
              sw.Write(textBox1.Text);
              sw.Close();
              }

              Any thoughts?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              electriac wrote:

              Any thoughts?

              Not only the stream is disposable, but also the streamwriter.

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

              1 Reply Last reply
              0
              • E electriac

                I got this piece of code from, I know not where, as a recommended way to write to a file.

                // better way to write to file stream
                using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
                {
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(textBox1.Text);
                sw.Close();
                }

                Any thoughts?

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #7

                Since you're just overwriting the file if it exists, try:

                File.WriteAllText(fileName, textBox1.Text);

                http://msdn.microsoft.com/en-us/library/system.io.file.writealltext.aspx[^]


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                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