我收到以下错误。
我需要卸载应用程序并删除应用程序或用户在程序文件/myapp 中创建的所有文件和文件夹。
我该如何解决这个问题?谢谢。
Access to the path 'C:\Program Files (x86)\DefaultProgram\Application\app.exe' is denied.
我的代码:
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);
}