检查Solidworks是否已安装?

7

我有一个能够在32位和64位操作系统上运行的C#应用程序。在我的应用程序中,如何通过编程方式检查计算机上是否安装了Solidworks。如果我们可以通过读取注册表键来检查它,则请为32位和64位提供路径。告诉我是否还有其他方法可以检查它。

4个回答

5
您可以按照以下方式使用WMI。
private static bool IsInstalled(string ProductName)
{

    bool rv = false;
    ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Product");
    ManagementObjectCollection Products = searcher.Get();
    if (Products.Count != 0)
    {
        foreach (ManagementObject product in Products)
        {
            if (product.Properties["Name"].Value.ToString() == ProductName)
            {
                rv = true;
            }
        }
    }
    return rv;           
}

1
我会在ManagementObjectSearcher和ManagementObjectCollection周围加上'using'语句,以确保它们被正确处理。 - SwDevMan81
当找到产品时,您可以通过将“rv = true”替换为“return true”来中断foreach循环(rv声明变得不必要)。 - alex

2

如果应用程序需要启动已安装的SolidWorks,我会使用以下方式启动所有独立(非插件)的SolidWorks工具:

Public swApp As SldWorks.SldWorks

Function GetSolidWorks(ForceLaunch As Boolean) As Boolean
    If Not swApp Is Nothing Then
        SetSolidWorksVisibility()
        Return True
    Else
        Try
            swApp = GetObject(, "SldWorks.Application")
            If swApp Is Nothing Then Return False

            SetSolidWorksVisibility()
            Return True
        Catch ex As Exception
            If Not ForceLaunch Then Return False

            swApp = CreateObject("SldWorks.Application")
            If swApp Is Nothing Then Return False

            SetSolidWorksVisibility()

            'simple timer to wait for solidworks to repond
            System.Threading.Thread.Sleep(5000)

            Return True
        End Try
    End If
End Function

Private Sub SetSolidWorksVisibility()
    If Not swApp.Visible Then swApp.Visible = True
    If Not swApp.FrameState = SwConst.swWindowState_e.swWindowMaximized Then swApp.FrameState = SwConst.swWindowState_e.swWindowMaximized
End Sub

0

我不确定macOS版本的Solidworks需要什么,但对于Windows来说,这应该是一种可靠的方法来检查Solidworks是否已安装。

我怀疑这将适用于2010年及以后的任何版本,因为Solidworks API帮助文档从那时开始。我已经测试过2018年及以后的版本。

using Microsoft.Win32;
using System.Runtime.InteropServices;

/// <summary>
/// Checks that Solidworks has the minimum required version installed 
/// </summary>
/// <param name="requiredVersion ">The minimum year of Solidworks required</param>
/// <exception cref="PlatformNotSupportedException"></exception>
public static bool CheckSolidworks(int requiredVersion = 2_021)
{
    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
    {
        var keyname = "SolidWorks";
        var registryKey = Registry.LocalMachine.OpenSubKey($"SOFTWARE\\{keyname}")
            ?? Registry.CurrentUser.OpenSubKey($"Software\\{keyname}");

        if (registryKey == null)
            return false;

        var matches = registryKey.GetSubKeyNames()?.Where(x => x.StartsWith("SOLIDWORKS"));

        if (matches == null)
            return false;

        int? installedVersion = null;

        foreach (var match in matches)
            if (int.TryParse(match[^4..], out int version) && (installedVersion == null || version > installedVersion))
                installedVersion = version;

        return installedVersion != null && installedVersion >= requiredVersion;
    }
    else 
    {
        throw new PlatformNotSupportedException("Method only supported on Windows");
    }
}

0



这是给初学者的...
我认为有很多种方法可以检查Solidworks是否已安装,但从我的角度来看,当Solidworks被安装时,它会在注册表中创建一些文件夹。

只需按照以下步骤进行检查...

打开运行
在其中输入regedit并按下 Enter
点击允许“用户访问控制”
进入 HKEY_LOCAL_MACHINE -> SOFTWARE

现在检查是否存在Solidwork文件夹条目
如果找到文件夹,则表示已安装Solidworks,否则未安装..!

希望这能帮助到您!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接