w3resource logo

:

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
 

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus