C# - How to Delete File And Folder Using C#
In This C# Tutorial We Will See How To Delete File Or A Folder From A Path String Using CSharp Programming Language.
Project Source Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form10 : Form
{
public Form10()
{
InitializeComponent();
}
private void BTN_RemoveFile_Click(object sender, EventArgs e)
{
string filepath = textBox1.Text;
if (File.Exists(filepath))
{
File.Delete(filepath);
MessageBox.Show("File Deleted");
}
else
{
MessageBox.Show("File Not Deleted");
}
}
private void BTN_RemoveFolder_Click(object sender, EventArgs e)
{
string folderepath = textBox2.Text;
if (Directory.Exists(folderepath))
{
Directory.Delete(folderepath, true);
MessageBox.Show("Folder Deleted");
}
else
{
MessageBox.Show("Folder Not Deleted");
}
}
}
}