Friday, 11 October 2013

How to read a file in C# .Net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace file
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = "my_text.txt";
 StreamReader r = new StreamReader(path);
            string s = r.ReadToEnd();
            foreach (var item in s)
            {
                Console.Write(item);


           }
// 2nd Way to read a file


            string str ="";
            while ((str = r.ReadLine())!=null)
                  {
                      Console.WriteLine(str);
           
           
            }
}
}
}

How to Append a file in C#.Net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace file
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = "my_text.txt";
           

            if (!File.Exists(path))
            {

                StreamWriter sw = new StreamWriter(path);

                sw.WriteLine("hello");

                sw.Close();
            }
            else
            {

                StreamWriter swr = new StreamWriter(path, true);
                swr.WriteLine("heheehheehe");
                swr.WriteLine("lallalaalal");
                swr.Close();

             }
         }
      }


            }

How to create a file in C# .Net

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace file
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = "my_text.txt";
           

            if (!File.Exists(path))
            {

                StreamWriter sw = new StreamWriter(path);

                sw.WriteLine("hello");

                sw.Close();
            }
   
        }
    }
}

Monday, 12 August 2013