Create and Delete text Files
How can I write code that delete a specific file? I have achieved this before in Visual Basic. But currently am working on a project that I need it to be able to create .txt files and delete then when exiting the application. Please note that when it comes to C# am an absolute beginner.
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.


Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted Jun 4, 2015, 4:11 PM EDT
Manoj BhoirRank #599 · 4K reputationPosted Jun 4, 2015, 9:31 AM EDT
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.FormClosing += new FormClosingEventHandler(Form1_Closing);
using (System.IO.FileStream fs = System.IO.File.Create("C:\\Test.txt"))
{
Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
}
{
System.IO.File.Delete("C:\\Test.txt");
}