Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

create
file
c#

How to create a file in C#

Educative Answers Team

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:

svg viewer

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:\ drive
        using (FileStream fs = File.Create(path))
    }
}

The FileStream class methods can be called, after the above code is executed​, with the fs object.

RELATED TAGS

create
file
c#
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring