C# create file
Edit
Introduction
The Create() method of the File class is used to create files in C#. The File.Create() method takes a fully specified path as a parameter and creates a file at the specified location; if any such file already exists at the given location, it is overwritten.
Example
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hello_World
{
class Program
{
static void Main(String[] args)
{
string fileName = @"C:\Users\Rakesh\Desktop\Rakesh.txt";
try
{
// Check if file already exists. If yes, delete it.
if (File.Exists(fileName))
{
File.Delete(fileName);
}
// Create a new file
using (FileStream fs = File.Create(fileName))
{
// Add some text to file
Byte[] title = new UTF8Encoding(true).GetBytes("Welcome ");
fs.Write(title, 0, title.Length);
byte[] author = new UTF8Encoding(true).GetBytes("MR Rakesh Kumar Sutar");
fs.Write(author, 0, author.Length);
}
// Open the stream and read it back.
using (StreamReader sr = File.OpenText(fileName))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine();
Console.WriteLine("\t" s);
}
}
}
catch (Exception Ex)
{
Console.WriteLine(Ex.ToString());
}
Console.ReadLine();
}
}
}