Delete Users Files

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

I'm trying to delete all the files in a users directory. I know there are
some that can't be deleted, that's ok. What I'm trying to do, is delete any
files that 'johnboy' might have saved to his folder when he was logged on
(Vista).
Here is my code:

private void DeleteFiles()
{
string dir = @"C:\users\johnboy\";
try
{
if (System.IO.Directory.Exists(dir))
{
string[] dirs = System.IO.Directory.GetDirectories(dir,
"*.*", System.IO.SearchOption.AllDirectories);
string[] files = System.IO.Directory.GetFiles(dir,
"*.*", System.IO.SearchOption.AllDirectories);
foreach (string file in files)
{
try
{
System.IO.File.Delete(file);
}
catch { }
}
foreach (string name in dirs)
{
try
{
System.IO.Directory.Delete(name, true);
}
catch { }
}
}
}
catch (Exception ex)
{
string err = ex.Message;
}
}

I get the exception: "Unable to delete files on the desktop: Access to the
path 'C:\users\johnboy\Desktop\' is denied". Problem is that it breaks out
of the foreach statement and quits deleting files. I really just want to
clean up as much as I can, any help is appreciated.
 
I'm trying to delete all the files in a users directory. I know there are
some that can't be deleted, that's ok. What I'm trying to do, is delete
any
files that 'johnboy' might have saved to his folder when he was logged on
(Vista).

Other than the fact that you might be running this from a form, this
question has nothing to do with Windows Forms. You might have better luck
asking in a C# or .NET Framework group.
 
Back
Top