Thursday 5 February 2015

Creating a New file without replacing existing file using IO Streams CreateNew

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

namespace Streams1
{
    class Program
    {
        static void Main(string[] args)
        {        
            string str = "H:\\yourfilename.doc";
            string str1 = "H:\\yourfilename";
            string str2 =".doc";
            //FileStream fs1 = new FileStream(str);
            int i = 0;
            while (File.Exists(str))
            {
                i++;
                str = str1 + i.ToString() + str2 ;
            }
            FileStream fs = new FileStream(str, FileMode.CreateNew, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            Console.WriteLine("wirte some datas into the str:");
            string str3 = Console.ReadLine();
            sw.Write(str3);
            sw.Flush();
            sw.Close();
            Console.ReadLine();
        }

    }
}

No comments:

Post a Comment