Search This Blog

Saturday, 16 July 2016

Write file in C#

    using System;
    using System.IO;

    class FileWrite
    {
        public void WriteData()
        {
            FileStream fs = new FileStream(@"C:\Test Folder\test.txt"FileMode.Open, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            string str = Console.ReadLine();
            sw.WriteLine(str);
            sw.Flush();
            sw.Close();
            fs.Close();
        }

        public static void Main(String[] args)
        {
            FileWrite fw = new FileWrite();
            fw.WriteData();

            Console.ReadLine();
        }
    }

Read file in C#

    using System;
    using System.IO;

    class FileRead
    {
        public void ReadData()
        {
            FileStream fs = new FileStream(@"C:\Test Folder\test.txt"FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);
            sr.BaseStream.Seek(0, SeekOrigin.Begin);
            string str = sr.ReadLine();
            while (str != null)
            {
                Console.WriteLine("{0}", str);
                str = sr.ReadLine();
            }
            sr.Close();
            fs.Close();

        }

        public static void Main(String[] args)
        {
            FileRead fr = new FileRead();
            fr.ReadData();

            Console.ReadLine();
        }
    }

OR

    using System;
    using System.IO;

    class FileRead
    {
        public void ReadData()
        {
            FileStream fs = new FileStream(@"C:\Test Folder\test.txt"FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);
            sr.BaseStream.Seek(0, SeekOrigin.Begin);
            string str = sr.ReadToEnd();
            Console.WriteLine("{0}", str);
            sr.Close();
            fs.Close();
        }

        public static void Main(String[] args)
        {
            FileRead fr = new FileRead();
            fr.ReadData();

 Console.ReadLine();
        }
    }

Check leap year in c#

    class LeapYear
    {
        public static void Main(string[] args)
        {
            int Year;

            Console.WriteLine("Enter the Year: ");
            Year = Convert.ToInt32(Console.ReadLine());
            if ((Year % 4 == 0) && (Year % 100 != 0 || Year % 400 == 0))
            {
                Console.WriteLine("The Year you have entered is a LeapYear: {0}", Year);
            }
            else
            {
                Console.WriteLine("The Year you have entered is not a Leap Year: {0}", Year);
            }
            Console.ReadLine();
        }
    }

Delete file in c#

using System.IO;

Delete respective file

string fileName = "test.txt";
string sourcePath = @"C:\Test Folder";
string sourceFile = Path.Combine(sourcePath, fileName);

if (File.Exists(sourceFile))
{
    File.Delete(sourceFile);
}

Delete all files

string sourcePath = @"C:\Test Folder";

string[] sourcefiles = Directory.GetFiles(sourcePath);

foreach (string sourcefile in sourcefiles)
{
    File.Delete(sourcefile);
}

Delete only .txt extension files

string sourcePath = @"C:\Test Folder";

string[] sourcefiles = Directory.GetFiles(sourcePath);

foreach (string sourcefile in sourcefiles)
{
    string ext = Path.GetExtension(sourcefile);

    if (ext.Equals(".txt"))
    {
        File.Delete(sourcefile);
    }
}

Move file from one folder to another in c#

using System.IO;

Move file from Root Folder to Sub Folder

string fileName = "test.txt";
string sourcePath = @"C:\Test Folder";
string targetPath = @"C:\Test Folder\Sub Folder";

string sourceFile = Path.Combine(sourcePath, fileName);
string destFile = Path.Combine(targetPath, fileName);

if (!Directory.Exists(targetPath))
{
    Directory.CreateDirectory(targetPath);
}

File.Move(sourceFile, destFile);    

Move file from one folder to another folder

string fileName = "test.txt";
string sourcePath = @"C:\Source Folder";
string targetPath = @"C:\Destination Folder";

string sourceFile = Path.Combine(sourcePath, fileName);
string destFile = Path.Combine(targetPath, fileName);

if (!Directory.Exists(targetPath))
{
    Directory.CreateDirectory(targetPath);
}

File.Move(sourceFile, destFile);    

Move file from one directories to another directories

string fileName = "test.txt";
string sourcePath = @"C:\Source Folder";
string targetPath = @"D:\Destination Folder";

string sourceFile = Path.Combine(sourcePath, fileName);
string destFile = Path.Combine(targetPath, fileName);

if (!Directory.Exists(targetPath))
{
    Directory.CreateDirectory(targetPath);
}

File.Move(sourceFile, destFile);    

Move all files from Root Folder to Sub Folder

string sourcePath = @"C:\Test Folder";
string targetPath = @"C:\Test Folder\Sub Folder";

if (!Directory.Exists(targetPath))
{
    Directory.CreateDirectory(targetPath);
}

string[] sourcefiles = Directory.GetFiles(sourcePath);

foreach (string sourcefile in sourcefiles)
{
    string fileName = Path.GetFileName(sourcefile);
    string destFile = Path.Combine(targetPath, fileName);

    File.Move(sourcefile, destFile);
}

Move all files from one folder to another folder

string sourcePath = @"C:\Source Folder";
string targetPath = @"C:\Destination Folder";

if (!Directory.Exists(targetPath))
{
    Directory.CreateDirectory(targetPath);
}

string[] sourcefiles = Directory.GetFiles(sourcePath);

foreach (string sourcefile in sourcefiles)
{
    string fileName = Path.GetFileName(sourcefile);
    string destFile = Path.Combine(targetPath, fileName);

    File.Move(sourcefile, destFile);
}

Move all files from one directories to another directories

string sourcePath = @"C:\Source Folder";
string targetPath = @"D:\Destination Folder";

if (!Directory.Exists(targetPath))
{
    Directory.CreateDirectory(targetPath);
}

string[] sourcefiles = Directory.GetFiles(sourcePath);

foreach (string sourcefile in sourcefiles)
{
    string fileName = Path.GetFileName(sourcefile);
    string destFile = Path.Combine(targetPath, fileName);

    File.Move(sourcefile, destFile);
}

Note:-

While moving files, if file already exists in destination folder. It’s throw error.