0

I tried to delete a folder at uninstall but it does nothing. specifying the path though, get it to work. Not working:

 public void OnAfterInstall(SetupEventArgs e)
        {
            if (e.IsUninstalling)
            {
                
                DirectoryInfo dir = new DirectoryInfo(@"CommonAppDataFolder\myFolder");
                if ((bool)(dir?.Exists))
                {
                    dir?.Delete(true);
                }
                
            }
        }

Working:

public void OnAfterInstall(SetupEventArgs e)
        {
            if (e.IsUninstalling)
            {
                
                DirectoryInfo dir = new DirectoryInfo(@"C:\ProgramData\myFolder");
                if ((bool)(dir?.Exists))
                {
                    dir?.Delete(true);
                }
                
            }
        }

Edit: I tried this code and I'm getting the very same error:

        var programFiles = Environment.ExpandEnvironmentVariables("%ProgramFiles%");
        var manifestFile = Path.Combine(programFiles,@"\My Company\myFile.man");

        FileInfo fInfo = new FileInfo(manifestFile);
        if ((bool)(fInfo?.Exists))
        {
            FileSecurity security = fInfo.GetAccessControl();
            security.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null),
                FileSystemRights.ReadAndExecute, AccessControlType.Allow));
            fInfo.SetAccessControl(security);
        }

        var CMDCommand = $@"/C wevtutil im {manifestFile}";
        Process.Start("CMD.exe", CMDCommand);

I tried also this:

var programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
    var manifestFile = programFiles + @"\My Company\myFile.man";

Running this code on C:\ is working fine..

1

1 Answer 1

1

You should expand the environment variable to get the actual path

var common = Environment.ExpandEnvironmentVariables("%ProgramData%");
var yourFolder = Path.Combine(common, "myFolder");
DirectoryInfo dir = new DirectoryInfo(yourFolder);

Notice how to to retrieve an Environment Variable from its string you need to put the string between %, also you could get an Hashtable with all the Environment Variables defined on your machine (custom or standard) using

var envs = Environment.GetEnvironmentVariables();

Consider also the alternative (or probably a better choice) highlighted by Hans Passant in its comment. Environment.GetFolderPath is a method that receives an enum SpecialFolder and returns the path for the special folder requested.

This is a better choice because it not depends on an Environment Variable that could be changed o removed and thus creating a possible buggy situation.

var common = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer, though it is not working.
Actually, Path.Combine(common, "myFolder"); return "myFolder"
Not sure what is the problem. Surely you should have an environment variable called %ProgramData%. Can you check the return value of Environment.GetEnvironmentVariables using the debugger?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.