Hi,

How can you create a text file in C# and write to it? Sometimes you may need to create a text file using C sharp program. Here is how you can do that.

using System;
using System.IO;

class MainClass
{
  static void Main(string[] args)
  {
    StreamWriter yourStream= null;
    string yourString= "I Love Coderz Heaven!";

    try
    {
      yourStream = File.CreateText("testFile.txt");
      yourStream.Write(yourString);
    }
    catch (IOException e)
    {
      Console.WriteLine(e);
    }
    catch (Exception e)
    {
      Console.WriteLine(e);
    }
    finally
    {
      if (yourStream!= null)
      {
          yourStream.Close();
      }
    }
  }
}

🙂

 

4 Comments

  1. daigoumee April 25, 2011 at 3:17 pm

    Useful blog website, keep me personally through searching it, I am seriously interested to find out another recommendation of it.

     
  2. Maya Adduci April 27, 2011 at 4:25 am

    Beneficial info and excellent design you got here! I want to thank you for sharing your ideas and putting the time into the stuff you publish! Great work!

     
  3. Malik April 27, 2011 at 12:12 pm

    @daigoumee -> Glad to see your comment. Thank you so much. We will always try to keep it best.

     
  4. Malik April 27, 2011 at 12:14 pm

    @Maya Adduci -> Very glad to hear that comment. Comments like this will always keep us motivated. Thank you sooooo much. 🙂

     

Leave a Reply

Your email address will not be published. Required fields are marked *

 

 

 

Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.