w3resource
C# Sharp Programming Exercises

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?



New Content: Composer: Dependency manager for PHP, R Programming