Example #1
0
        public static void ClearApplicationTempFolder()
        {
            string tmpFolder = GetDefaultApplicationTempFolder();

            if (System.IO.Directory.Exists(tmpFolder))
            {
                try
                {
                    System.IO.Directory.Delete(tmpFolder, true);
                }
                catch (System.Security.SecurityException)
                {
                    System.IO.DirectoryInfo tmpPathDirInfo = new System.IO.DirectoryInfo(tmpFolder);
                    System.Security.AccessControl.DirectorySecurity dSecurity = tmpPathDirInfo.GetAccessControl();
                    try
                    {
                        bool tryModify = false;
                        dSecurity.ModifyAccessRule(System.Security.AccessControl.AccessControlModification.Reset, new System.Security.AccessControl.FileSystemAccessRule("everyone", System.Security.AccessControl.FileSystemRights.FullControl, System.Security.AccessControl.InheritanceFlags.ObjectInherit | System.Security.AccessControl.InheritanceFlags.ContainerInherit, System.Security.AccessControl.PropagationFlags.None, System.Security.AccessControl.AccessControlType.Allow), out tryModify);
                        if (tryModify)
                        {
                            tmpPathDirInfo.SetAccessControl(dSecurity);
                            tmpPathDirInfo.Delete(true);
                        }
                        else
                        {
                            dSecurity.AddAccessRule(new System.Security.AccessControl.FileSystemAccessRule("everyone", System.Security.AccessControl.FileSystemRights.FullControl, System.Security.AccessControl.InheritanceFlags.ObjectInherit | System.Security.AccessControl.InheritanceFlags.ContainerInherit, System.Security.AccessControl.PropagationFlags.None, System.Security.AccessControl.AccessControlType.Allow));
                            tmpPathDirInfo.SetAccessControl(dSecurity);
                            tmpPathDirInfo.Delete(true);
                        }
                    }
                    catch
                    {
                    }
                }
                catch
                {
                }
            }
        }