Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Remove a file from the disk

C# Sharp File Handling: Exercise-2 with Solution

Write a program in C# Sharp to remove a file from the disk.

Sample Solution:-

C# Sharp Code:

using System;
using System.IO;
using System.Text;

class filexercise1
{
    public static void Main()
    {
        string fileName = @"mytest.txt";
			Console.Write("\n\n Remove a file from the disk (at first create the file ) :\n");	
			Console.Write("--------------------------------------------------------------\n");				

            // Delete the file if it exists.
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            Console.WriteLine(" The file {0} deleted successfully.\n\n",fileName);
            }
            else
            {
            Console.WriteLine(" File does not exist");
            Console.ReadKey();
            }
    }
}

Sample Output:

Remove a file from the disk (at first create the file ) :                                                    
--------------------------------------------------------------                                                
 File does not exist
 

Flowchart:

Flowchart: C# Sharp Exercises - Remove a file from the disk.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a program in C# Sharp to create a blank file in the disk newly.
Next: Write a program in C# Sharp to create a blank file in the disk if the same file already exists.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.