public static UninstallPackage ( string pkgId ) : void | ||
pkgId | string | Package Id |
Résultat | void |
public static void UninstallPackage(string pkgId)
{
var installedFiles = BlogService.InstalledFromGalleryPackageFiles(pkgId);
var pkg = PackageRepository.GetPackage(pkgId);
foreach (var file in installedFiles.OrderByDescending(f => f.FileOrder))
{
var fullPath = HttpContext.Current.Server.MapPath(Path.Combine(Utils.RelativeWebRoot, file.FilePath));
if(file.IsDirectory)
{
var folder = new DirectoryInfo(fullPath);
if (folder.Exists)
{
if(folder.GetFileSystemInfos().Length == 0)
{
ForceDeleteDirectory(fullPath);
}
else
{
Utils.Log(string.Format("Package Uninstaller: can not remove directory if it is not empty ({0})", fullPath));
}
}
}
else if (File.Exists(fullPath))
{
File.Delete(fullPath);
}
}
if (!string.IsNullOrWhiteSpace(pkg.LocalVersion))
{
var pkgDir = string.Format("{0}.{1}", pkgId, pkg.LocalVersion);
// clean up removing installed version
pkgDir = HttpContext.Current.Server.MapPath(string.Format("{0}App_Data/packages/{1}", Utils.ApplicationRelativeWebRoot, pkgDir));
if (Directory.Exists(pkgDir))
{
ForceDeleteDirectory(pkgDir);
}
}
}
/// <summary> /// Uninstall package /// </summary> /// <param name="pkgId"></param> /// <returns></returns> public static bool UninstallPackage(string pkgId) { try { FileSystem.UninstallPackage(pkgId); Utils.Log(string.Format("Uninstalled package {0}: installed package files removed. ", pkgId)); // remove packagefiles.xml and packages.xml (or DB records) BlogService.DeletePackage(pkgId); Utils.Log(string.Format("Uninstalled package {0}: package records removed. ", pkgId)); UninstallGalleryPackage(pkgId); Utils.Log(string.Format("Uninstalled package {0}: NuGet file removed. ", pkgId)); // reset cache Blog.CurrentInstance.Cache.Remove(Constants.CacheKey); Utils.Log(string.Format("Uninstalled package {0} by {1}", pkgId, Security.CurrentUser.Identity.Name)); } catch (Exception ex) { Utils.Log(string.Format("Error unistalling package {0}: {1}"), pkgId, ex.Message); throw; } return(true); }