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. Writing to a Text File

Writing to a Text File

Scheduled Pinned Locked Moved C#
questioncsharpgraphicshelp
3 Posts 3 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.
  • D Offline
    D Offline
    dsl fahk
    wrote on last edited by
    #1

    I am new to codeproject, and relatively new to C#. Eventually i got bored of... non-saving uses... and decided i needed to save something. So i tried.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;

    namespace FileMaker
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

        private void btnCreate\_Click(object sender, EventArgs e)
        {
            Stream myStream;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    
            saveFileDialog1.Filter = "txt files (\*.txt)|\*.txt|All files (\*.\*)|\*.\*";
            saveFileDialog1.FilterIndex = 1;
            saveFileDialog1.RestoreDirectory = true;
    
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = saveFileDialog1.OpenFile()) != null)
                {
                    StreamWriter wText = new StreamWriter(myStream);
                    wText.Write("CHEESE");
    
                    myStream.Close();
                }
            } 
        }
    }
    

    }

    It doesn't work. When i run the program and click on the button, and go through the save file dialog, a file does appear, but "CHEESE" does not appear in the text file. I cannot figure out why this doesn't work. So how do i fix this so that "CHEESE" appears in the file? Thanks to anybody who helps!

    L 1 Reply Last reply
    0
    • D dsl fahk

      I am new to codeproject, and relatively new to C#. Eventually i got bored of... non-saving uses... and decided i needed to save something. So i tried.

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Text;
      using System.Windows.Forms;
      using System.IO;

      namespace FileMaker
      {
      public partial class Form1 : Form
      {
      public Form1()
      {
      InitializeComponent();
      }

          private void btnCreate\_Click(object sender, EventArgs e)
          {
              Stream myStream;
              SaveFileDialog saveFileDialog1 = new SaveFileDialog();
      
              saveFileDialog1.Filter = "txt files (\*.txt)|\*.txt|All files (\*.\*)|\*.\*";
              saveFileDialog1.FilterIndex = 1;
              saveFileDialog1.RestoreDirectory = true;
      
              if (saveFileDialog1.ShowDialog() == DialogResult.OK)
              {
                  if ((myStream = saveFileDialog1.OpenFile()) != null)
                  {
                      StreamWriter wText = new StreamWriter(myStream);
                      wText.Write("CHEESE");
      
                      myStream.Close();
                  }
              } 
          }
      }
      

      }

      It doesn't work. When i run the program and click on the button, and go through the save file dialog, a file does appear, but "CHEESE" does not appear in the text file. I cannot figure out why this doesn't work. So how do i fix this so that "CHEESE" appears in the file? Thanks to anybody who helps!

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      try closing the streamwriter before closing the stream... :)

      Luc Pattyn

      K 1 Reply Last reply
      0
      • L Luc Pattyn

        try closing the streamwriter before closing the stream... :)

        Luc Pattyn

        K Offline
        K Offline
        karam chandrabose
        wrote on last edited by
        #3

        wText.Close(); MyStream.Close(); unless u close the writer, buffered things will not be explicitly fwded to the disk.

        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