How to create a file in C#
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.
Syntax
The syntax for the File.Create() method is:
Code
The code snippet below illustrates the process of creating a file at a specified path with the File.Create() method.
using System;using System.IO;using System.Text;class FileCreation{public static void Main(){string path = @"D:\Python_Files\my_file.py";// The line below will create a file my_file.py in// the Python_Files folder in D:\ driveusing (FileStream fs = File.Create(path))}}
The
FileStreamclass methods can be called, after the above code is executed, with thefsobject.
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved