using Autodesk.Revit.DB; using Autodesk.RevitAddIns; using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Configuration.Install; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using static Autodesk.Internal.Windows.SwfMediaPlayer; namespace AddinDeployer { [RunInstaller(true)] public class CustomActionClass : Installer { //public static void Main(string[] args) //{ // ProcessEvent(); //} private string path; private static string assemblyname = "RookieStation"; private static string addinname = "RookieStation.addin"; private static string dllname = "RookieStation.dll"; private string fullclassname1 = "RookieStation.RibbonMenu.RsApp"; public CustomActionClass() { this.BeforeInstall += CustomActionClass_BeforeInstall; this.BeforeUninstall += CustomActionClass_BeforeUninstall; //this.AfterInstall += CustomActionClass_AfterInstall; //this.AfterUninstall += CustomActionClass_AfterUninstall; } //private string revitvernum = "2020"; public static void KillProcess(string strProcessesByName)//关闭线程 { foreach (Process p in Process.GetProcesses())//GetProcessesByName(strProcessesByName)) { if (p.ProcessName.ToUpper().Contains(strProcessesByName)) { try { p.Kill(); p.WaitForExit(); // possibly with a timeout } catch (Win32Exception e) { MessageBox.Show(e.Message.ToString()); } catch (InvalidOperationException e) { MessageBox.Show(e.Message.ToString()); } } } } private static void CustomActionClass_BeforeUninstall(object sender, InstallEventArgs e) { KillProcess("REVIT"); //var dialogResult = System.Windows.Forms.MessageBox.Show("是否关闭Revit以卸载插件", "提示", System.Windows.Forms.MessageBoxButtons.YesNo); //if (dialogResult == System.Windows.Forms.DialogResult.Yes) //{ //} //while (MessageBox.Show("退出当前窗体?", "", MessageBoxButtons.YesNo) == DialogResult.No) //{ // for (int j = 1; j <= i; j++) // { // listBox1.Items.Add(j); // } //} //System.Environment.Exit(System.Environment.ExitCode); //var ps = Process.GetProcessesByName("Revit.exe"); //foreach (var p in ps) //{ // p.Kill(); //} } private static void CustomActionClass_BeforeInstall(object sender, InstallEventArgs e) { KillProcess("REVIT"); try { var path = @"C:\ProgramData\Autodesk\Revit\Addins\2020"; //DelAddinFromPath(path); DeleteFolder(path + "\\RsTools"); } catch (Exception) { } //var dialogResult = System.Windows.Forms.MessageBox.Show("是否关闭Revit以安装插件", "提示", System.Windows.Forms.MessageBoxButtons.YesNo); //if (dialogResult == System.Windows.Forms.DialogResult.Yes) //{ //} //var ps = Process.GetProcessesByName("Revit.exe"); //foreach (var p in ps) //{ // p.Kill(); //} //else //{ // //this.Exit() //} } private static void CustomActionClass_AfterInstall(object sender, InstallEventArgs e) { } private static void CustomActionClass_AfterUninstall(object sender, InstallEventArgs e) { //DeleteFolder(path); //DelAddinFromPath("C:\\ProgramData\\Autodesk\\Revit\\Addins"); List revitProducts = RevitProductUtility.GetAllInstalledRevitProducts(); foreach (var p in revitProducts) { string version = p.Version.ToString(); string versionnum = version.Trim("Revit".ToCharArray()); AddInInstaller addin = new AddInInstaller(versionnum); addin.Uninstall(); } } public static void DeleteFolder(string dir) { if (Directory.Exists(dir)) //如果存在这个文件夹删除之 { foreach (string d in Directory.GetFileSystemEntries(dir)) { if (File.Exists(d)) File.Delete(d); //直接删除其中的文件 else DeleteFolder(d); //递归删除子文件夹 } Directory.Delete(dir, true); //删除已空文件夹 } } public static void DelAddinFromPath(string dir) { if (Directory.Exists(dir)) { foreach (string d in Directory.GetFileSystemEntries(dir)) { if (File.Exists(d) && d.EndsWith(addinname)) File.Delete(d); //直接删除其中的文件 else DelAddinFromPath(d); } } } public override void Rollback(IDictionary savedState) { base.Rollback(savedState); } public override void Commit(IDictionary savedState) { base.Commit(savedState); } public override void Install(IDictionary stateSaver) { base.Install(stateSaver); } public override void Uninstall(IDictionary savedState) { base.Uninstall(savedState); } } internal class AddInInstaller { private string addinname = "RookieStation.addin"; private string vendorid = "YTX"; private string vendorDescription = "YTX"; internal AddInInstaller(string revitVersion) { this.Manifest = new RevitAddInManifest(); this.ManifestPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Autodesk\\Revit\\Addins", revitVersion, addinname); } private RevitAddInManifest Manifest { get; } private string ManifestPath { get; } internal void CreateApplication(string name, string assemblyPath, string fullClassName) { RevitAddInApplication item = new RevitAddInApplication(name, assemblyPath, Guid.NewGuid(), fullClassName, vendorid) { VendorDescription = vendorDescription }; this.Manifest.AddInApplications.Add(item); } internal void CreateCommand(string assemblyPath, string fullClassName) { RevitAddInCommand item = new RevitAddInCommand(assemblyPath, Guid.NewGuid(), fullClassName, vendorid) { VendorDescription = vendorDescription }; this.Manifest.AddInCommands.Add(item); } internal void Install() { DirectoryInfo directory = new FileInfo(this.ManifestPath).Directory; if (directory == null) { return; } if (!directory.Exists) { Directory.CreateDirectory(directory.FullName); } this.Manifest.SaveAs(this.ManifestPath); } internal void Uninstall() { if (File.Exists(this.ManifestPath)) { File.Delete(this.ManifestPath); } } } }