0

I am getting the below error.

I need to uninstall the app and delete all files and folders created either by the application or users in the program files/myapp.

How can I resolve this issue? Thanks.

Access to the path 'C:\Program Files (x86)\DefaultProgram\Application\app.exe' is denied.

My Code:

protected override void OnAfterUninstall(IDictionary savedState)
        {
            string sFolder = Path.GetDirectoryName(Context.Parameters["assemblypath"]);
            string sUsername = "NT AUTHORITY\\LOCALSERVICE";
            DirectoryInfo myDirectoryInfo = new DirectoryInfo(sFolder);
            DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();
            myDirectorySecurity.AddAccessRule(
                new FileSystemAccessRule(
                    sUsername, FileSystemRights.Read | 
                    FileSystemRights.Write | 
                    FileSystemRights.Modify, InheritanceFlags.ContainerInherit | 
                    InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));
            myDirectoryInfo.SetAccessControl(myDirectorySecurity);
            base.OnAfterUninstall(savedState);
            string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            DeleteDirectory(Path.Combine(appPath, "DB"));
            DeleteDirectory(appPath);
        }

        public static void DeleteDirectory(string target_dir)
        {
            string[] files = Directory.GetFiles(target_dir);
            string[] dirs = Directory.GetDirectories(target_dir);
            foreach (string file in files)
            {
                File.SetAttributes(file, FileAttributes.Normal);
                File.Delete(file);
            }
            foreach (string dir in dirs)
            {
                DeleteDirectory(dir);
            }
            Directory.Delete(target_dir, false);
        } 
kyusan93
  • 312
  • 1
  • 7
  • 19

3 Answers3

2

Your main Problem is UAC in Windows Vista and Later Windows Versions!

Your Code will run correctly in XP but create complications in Vista or 7 or 8.

See the Answers Here to run your Application as Administrator!

Community
  • 1
  • 1
Writwick
  • 2,133
  • 6
  • 23
  • 54
  • Do you have other ways? I try not to use process method – kyusan93 May 14 '12 at 08:32
  • Is the "app.exe" is the application you are developing? – Writwick May 14 '12 at 08:34
  • If "app.exe is not your application, I thick UAC [User Account Control] is blocking the Deletion! Try running your application as admin! – Writwick May 14 '12 at 08:36
  • Hi, the app.exe is my application. Actually I am doing the uninstall method and I like to have all files and folders to be deleted after uninstall of my app. – kyusan93 May 14 '12 at 08:40
  • if you are trying to delete app.exe from inside of app.exe it wont work as the app.exe is still running..if you are deleting the app.exe from any other application coded by you,first you should enable UAC for your app that you are using to uninstall your app.exe [Run as Admin]. – Writwick May 14 '12 at 08:51
  • See [Here](http://stackoverflow.com/q/2818179/1118933) to run your Application as Administrator! Editing my Answer! – Writwick May 14 '12 at 08:59
1

You can delete your app.exe in this way on XP

Process.Start("cmd.exe", "/C ping 1.1.1.1 -n 1 -w 3000 > Nul & Del " + 
      Application.ExecutablePath); 
Application.Exit();

or this in Win7 (EDIT2)

Process.Start("cmd.exe", "timeout 5 > Nul & Del " + 
      Application.ExecutablePath); 
Application.Exit();

But you must add in foreach if(file.Contains("app.exe")) continue;

EDIT

protected override void OnAfterUninstall(IDictionary savedState)
    {
        base.OnAfterUninstall(savedState);
        string sFolder = Path.GetDirectoryName(Context.Parameters["assemblypath"]);
        string sUsername = "NT AUTHORITY\\LOCALSERVICE";
        DirectoryInfo myDirectoryInfo = new DirectoryInfo(sFolder);
        DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();
        myDirectorySecurity.AddAccessRule(
            new FileSystemAccessRule(
                sUsername, FileSystemRights.Read | 
                FileSystemRights.Write | 
                FileSystemRights.Modify, InheritanceFlags.ContainerInherit | 
                InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));
        myDirectoryInfo.SetAccessControl(myDirectorySecurity);
        string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
       DeleteDirectory(Path.Combine(appPath, "DB"));
        DeleteDirectory(appPath);
    }

    public static void DeleteDirectory(string target_dir)
    {
        string[] files = Directory.GetFiles(target_dir);
        string[] dirs = Directory.GetDirectories(target_dir);
        foreach (string file in files)
        {
            File.SetAttributes(file, FileAttributes.Normal);
            if(file.Contains("app.exe")) continue;
            File.Delete(file);
        }
        foreach (string dir in dirs)
        {
            DeleteDirectory(dir);
        }
        Directory.Delete(target_dir, false);
    } 
Likurg
  • 2,742
  • 17
  • 22
  • Do you have other ways? I try not to use process method – kyusan93 May 14 '12 at 08:29
  • 1
    @kyusan93 From which processes, you called delete function? – Likurg May 14 '12 at 08:50
  • 1
    I mean this function called from app.exe or somewhere else? – Likurg May 14 '12 at 09:19
  • app.exe is the main app... I uninstall using the uninstall.msi which I had created with the installer class. – kyusan93 May 14 '12 at 09:27
  • @kyusan93 try to delete override of OnAfterUninstall. And start uninstall.msi. Tell me please, what will happen with app.exe – Likurg May 14 '12 at 09:57
  • Hi, if i delete onafteruninstall, the app will be deleted and uninstalled, but i will still have the folders left behind for those folders or files created after installation in the program files/myapp folder – kyusan93 May 14 '12 at 10:15
  • Let me try when i have my laptop... Thanks... Maybe i will try another way... To let app or user create folders or files in another folder other than the app.path – kyusan93 May 14 '12 at 10:39
  • @kyusan93 i think best way to make all files and folders in user's docs. Good luck. – Likurg May 14 '12 at 10:44
  • Why are you using ping? That's ridiculous, just use sleep. – Mahmoud Al-Qudsi May 14 '12 at 18:04
  • @MahmoudAl-Qudsi its trick cause there is no sleep in cmd, so please remove your down vote btw you can find it here http://stackoverflow.com/questions/1672338/how-to-sleep-for-5-seconds-in-windowss-command-prompt-or-dos – Likurg May 14 '12 at 18:37
1

Your problem is related to security, for example in windows XP it might work while in Windows 7 it might fail and you will be denied of manipulating files.

In order to overcome the problem you need to make sure of the followings:

  1. package and deployment project output two files: .msi & .exe.

    .MSI file - is allowed to execute under some kind of pre-defined specifically for installers privileges - and that means it may not have enough privileges to delete file from any folder you want - it can only add or remove files from your target application folder.

    .Exe file - almost do the same as MSI file, it actually being set to execute the msi file. Yet.. since it is Exe file you can set a flag to instruct it must be execute under administrator privileges and that will allow you to achieve your goal. (.exe may have other stuff you may need to deploy i.e .net package or directX)

  2. since the situation is very inconvenient in the sense that you are being pushed to provide your users with the two files and they will not know what to execute.. I recommend that you to use WINRAR to archive both files into a RAR self extracting archive. you can set the RAR archive to execute the archived .EXE file in administrator privilege and set the RAR-sfx archive itself (which is also .exe) with the flag "require administrator privileges".

Félix LD
  • 372
  • 1
  • 5
  • 19
G.Y
  • 6,042
  • 2
  • 37
  • 54