只能被打开一次,判断此电脑是否安装NavisWorks
static void Main(string[] args)
{
if (checkNavisWorks() != true)
{
MessageBox.Show("Not Install NavisWorks!!!");
return;
}
bool ret;
System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application.ProductName, out ret);
if (!ret)
{
MessageBox.Show(null, "有一个和本程序相同的应用程序已经在运行,请不要同时运行多个本程序。\n这个程序即将退出。", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
Application.Exit();
}
else
{
System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
Autodesk.Navisworks.Api.Automation.NavisworksApplication navisworksApplication = null;
try
{
if (navisworksApplication == null)
{
navisworksApplication = new Autodesk.Navisworks.Api.Automation.NavisworksApplication();
navisworksApplication.Visible = false;
navisworksApplication.OpenFile(filePathName);
navisworksApplication.ExecuteAddInPlugin("MyTest.ADSK", str);
navisworksApplication = null;
}
else
{
navisworksApplication = new Autodesk.Navisworks.Api.Automation.NavisworksApplication();
navisworksApplication.Visible = false;
navisworksApplication.OpenFile(filePathName);
navisworksApplication.ExecuteAddInPlugin("MyTest.ADSK", str);
navisworksApplication = null;
}
}
catch (Autodesk.Navisworks.Api.Automation.AutomationDocumentFileException e)
{
System.Windows.Forms.MessageBox.Show("Error: " + e.Message);
}
catch (Autodesk.Navisworks.Api.Automation.AutomationException e1)
{
System.Windows.Forms.MessageBox.Show("Error: " + e1.Message);
}
finally
{
if (navisworksApplication != null)
{
navisworksApplication.Dispose();
navisworksApplication = null;
}
}
}
}
private static bool checkNavisWorks()
{
bool result = false;
try
{
Microsoft.Win32.RegistryKey uninstallNode = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
foreach (string subKeyName in uninstallNode.GetSubKeyNames())
{
Microsoft.Win32.RegistryKey subKey = uninstallNode.OpenSubKey(subKeyName);
object displayName = subKey.GetValue("DisplayName");
if (displayName != null)
{
if (displayName.ToString().Contains("Autodesk Navisworks Manage"))
{
result = true;
}
}
}
}
catch (Exception e)
{
string error = $"error in CkeckNaviswork:{e.Message}";
throw new Exception(error);
}
return result;
}