Click here to Skip to main content
15,913,722 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace IS_Application
{
    public partial class frmMain : Form
    {

        public frmMain()
        {
            InitializeComponent();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            
             try
            {
                string user = textBox1.Text;
                DirectoryInfo username = new DirectoryInfo("C:\\Documents and Settings\\" + user);

                if (Directory.Exists(@"C:\Documents and Settings\" + user))

                File.Delete("C:\\Documents and Settings\\" + user + "\\Local Settings\\Temp\\");
                File.Delete("C:\\Documents and Settings\\" + user + "\\Local Settings\\Temp\\Temporary Internet Files\\");
                File.Delete("C:\\Documents and Settings\\" + user + "\\Application Data\\");

                //Directory.Delete("C:\\Documents and Settings\\" + user + "\\Local Settings\\Temp\\");
                //Directory.Delete("C:\\Documents and Settings\\" + user + "\\Local Settings\\Temp\\Temporary Internet Files\\");
            }
            catch (Exception)
            {
                throw;
            }
         }
     }
}
Posted
Updated 27-Jul-11 14:41pm
v2
Comments
Philippe Mori 27-Jul-11 23:52pm    
Very bad idea to do that... You should not touch files that belongs to other users. Respect them.

Possible reason 1:
You will get an access denied error if the files you are tryingdelete are "Read
Only". If you get rid of the read only attribute and the files will be
deleted.

Possible reason 2:

Is the file in use? It sounds obvious but it happens. If it is being used or open, close the file and the application that opened it. For example, if it is a Word document, close Microsoft Word.

Possible reason 3:
If the file was opened in an application (and subsequently closed), but the program is still running, try quitting the program. Windows will lock a file because the application hasn't yet released it. This is not always Windows fault and can be the fault of the program.
 
Share this answer
 
Comments
walterhevedeich 27-Jul-11 22:22pm    
Good point.
Philippe Mori 27-Jul-11 23:50pm    
Also since the file is to another user, it might be possible that the security settings restrict what you are allowed to do with someone else file.
Orcun Iyigun 28-Jul-11 0:12am    
Yes that is another option as well.
You cannot delete stuff in the Temporary Internet Files folder. That folder is managed by Internet Explorer and must be cleaned out using IE's interface.
 
Share this answer
 
Comments
Philippe Mori 27-Jul-11 23:43pm    
Even if you can, you should not...
It does not make sense to create such an application. Are you a virus writer. You should not touch files owned by other users. It is not safe to delete "Application data" as some application uses that folder for permanent data that should not be deleted.

You application will not works on Windows Vista and latter and will also fails on some XP system.

If you want to do some cleanup to recuperate some space, Windows already has a tool for that. Use it. Windows knows better that you where temporary files are stored either for the current user or any users.

Also when an account is deleted, it is possible to ask Windows to perform cleanup.

Finally, hard drives are so cheap these day that it does not make sense to do this kind of cleanup except maybe once every few months using tools provide by Windows (Right-click on a drive in Windows Explorer and select Properties then you will have access to options for cleaning).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900