Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] How to go about deleting files within a directory?

Started by foolios Nov 30, 2007 at 3:24 PM 3 replies 2.7k views
Original Post
foolios
foolios
I am wondering what part of the language to look into to delete files that are in a directory. I would like to try to make my small app have this functionality. Is it possible to have deletion occur to certain file types in that directory as well? Thanks in advance
I rate users. Its just not having much impact as my rating is low...
Geronimo2000
Geronimo2000
Here's an example.

 foreach(string filename in System.IO.Directory.GetFiles(directoryName, "*.txt")){     System.IO.File.Delete(filename);}
foolios
foolios
Thank you!
I rate users. Its just not having much impact as my rating is low...
foolios
foolios
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace DeletingFiles
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
string path = @"C:\Documents and Settings\Administrator\Local Settings\Application Data\Mozilla\Firefox\Profiles\8lrj220l.default\Cache";
string target = @"C:\Documents and Settings\Administrator\Local Settings\Temporary Internet Files";

try
{
if (Directory.Exists(target))
{
label1.Text = "IE has: " + Directory.GetFiles(target).Length.ToString();
foreach (string filename in System.IO.Directory.GetFiles(target, "*.gif"))
{
System.IO.File.Delete(filename);
}
}

if (Directory.Exists(path))
{
label1.Text += "\nFireFox has: " + Directory.GetFiles(path).Length.ToString();
}

}
catch (Exception)
{
label1.Text = "The process failed.";
}
finally { }
}
}
}

Files still there. Or else I deleted something else by accident. ;x
I rate users. Its just not having much impact as my rating is low...
foolios
foolios
It seems to work if I change the directory. What kind of problem could I be having with the temporary internet files directory for ie? I am logged in as administrator.
I rate users. Its just not having much impact as my rating is low...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.