w3resource logo


C# Sharp Programming Exercises

C# Sharp Exercises: Remove a file from the disk

Secondary Nav

C# Sharp File Handling : Exercise-2 with Solution

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

 Remove a file from the disk (at first create the file ) :                                                    
--------------------------------------------------------------                                                
 The file mytest.txt deleted successfully.
 
  Remove a file from the disk (at first create the file ) :                                                    
--------------------------------------------------------------                                                
 File does not exist 

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();
            }
    }
}

C# Sharp Practice online:


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();
            }
    }
}

Improve this sample solution and post your code through Disqus.



Join our Question Answer community to learn and share your programming knowledge.

Solve these problems:

Java: How to convert a string to an integer in Java?

C#: Loops in c#

SQL: JOIN using more than 5 tables