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:
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.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework