[C# Code Sample] C# Program to Create File

Create file in C# | C# Program to Create File

In this article, find the C# code sample to create the text file System.IO library.


C# code sample to create file:

In the below example, you can see a c# sample code that creates a new file D:\myfile.txt.

.
/*
 * C# Program to Create a File
 */
using System;
using System.IO;
using System.Text;
class Test
{
    public static void Main()
    {
        string sFilePath = @"D:\Files\myfile.txt";
        using (FileStream fs = File.Create(sFilePath ))
        {
            Byte[] info = new UTF8Encoding(true).GetBytes("File is Created");
            fs.Write(info, 0, info.Length);
        } 
        using (StreamReader sr = File.OpenText(sFilePath ))
        {
            string s = "";
            while ((s = sr.ReadLine()) != null)
            {
                Console.WriteLine(s);
            }
        }
        Console.Read();
    }
}


– Article ends here –

If you have any questions, please feel free to share your questions or comments on the comment box below.

Recommended:   How to list all the dependent windows services using C#
Share this:
We will be happy to hear your thoughts

Leave a reply

Troubleshootyourself
Logo