using System; using Microsoft.Win32; using System.Collections.Generic; using System.IO; using System.Windows.Forms; namespace StartApp { public enum CadType { eCadType_NONE = 0, eCadType_AutoCAD = 1, eCadType_ZWCAD = 2, }; class CADVerInfo { public CADVerInfo() { m_sCADName = string.Empty; m_sMainRegKey = string.Empty; m_sSubRegKey = string.Empty; m_sCADLocation = string.Empty; m_iMainVer = 0; m_iSubVer = 0; m_CADType = CadType.eCadType_NONE; m_sExeName = string.Empty; } public string m_sExeName { get; set; } public CadType m_CADType { get; set; } // 产品名称 (Autocad 2014 simple chinese) public string m_sCADName { get; set; } // 注册表地址 public string m_sMainRegKey { get; set; } //R19.1\\ACAD-D001:8.4 public string m_sSubRegKey { get; set; } // ACAD-D001:8.4 // 安装路径 public string m_sCADLocation { get; set; } //c:\\autocad // 主版本号 public int m_iMainVer { get; set; } //19 public int m_iSubVer { get; set; } //1 public string m_sVerStr { get; set; } private bool setVersion(string input, CadType type) { if (type == CadType.eCadType_AutoCAD) { m_CADType = CadType.eCadType_AutoCAD; m_sExeName = "acad.exe"; // 基础格式校验 if (string.IsNullOrEmpty(input) || input[0] != 'R' || input.Length < 2) return false; // 查找小数点位置 int dotIndex = input.IndexOf('.'); // 分割整数和小数部分 string integerStr = ""; string decimalStr = ""; if (dotIndex == -1) { // 没有小数点时,整个R后的内容为整数部分 integerStr = input.Substring(1); } else { // 提取整数部分(R后到小数点前) if (dotIndex > 1) integerStr = input.Substring(1, dotIndex - 1); // 提取小数部分(小数点后到结尾) if (input.Length > dotIndex + 1) decimalStr = input.Substring(dotIndex + 1); } // 尝试转换为int类型 if (int.TryParse(integerStr, out int integerPart) && int.TryParse(decimalStr, out int decimalPart)) { m_iMainVer = integerPart; m_iSubVer = decimalPart; m_sVerStr = string.Format("R{0}{1}", m_iMainVer, m_iSubVer); return true; } } else { // 基础格式校验 if (string.IsNullOrEmpty(input) || input.Length < 2) return false; m_CADType = CadType.eCadType_ZWCAD; m_sExeName= "zwcad.exe"; string s; int dotIndex = input.IndexOf('.'); if (dotIndex == -1) { s = input.Substring(0); } else { s = input.Substring(0, dotIndex); } if (int.TryParse(s, out int integerPart)) { m_iMainVer = integerPart; m_iSubVer = 0; m_sVerStr = string.Format("Z{0}", m_iMainVer); return true; } } return false; } //////////////////////////////////////////////////////////////// private static bool getAutoCadList(ref List cadLists) { string regPath = @"software\autodesk\autocad"; try { using (RegistryKey keyCad = Registry.LocalMachine.OpenSubKey(regPath)) { if (keyCad == null) return false; // 继续遍历 R18.2 下面子级别 foreach (var version in keyCad.GetSubKeyNames()) { using (RegistryKey keyVersion = keyCad.OpenSubKey(version)) { if (keyVersion == null) continue; foreach (var lang in keyVersion.GetSubKeyNames()) { using (RegistryKey keyLang = keyVersion.OpenSubKey(lang)) { if (keyLang == null) continue; var location = keyLang.GetValue("AcadLocation"); // 判断注册表项是否有效 if (location == null) continue; if (Directory.GetFiles(location.ToString(), "acad.exe").Length == 0) continue; CADVerInfo info = new CADVerInfo(); info.setVersion(version, CadType.eCadType_AutoCAD); info.m_sMainRegKey = $"{version}\\{lang}"; info.m_sSubRegKey = lang; info.m_sCADLocation = location.ToString(); object ProductName = keyLang.GetValue("ProductName"); if (ProductName != null) info.m_sCADName = ProductName.ToString(); cadLists.Add(info); // 一个版本只有一个key是有效的,不需要继续遍历了 break; } } } } } } catch (Exception ex) { MessageBox.Show($"无法访问: {ex.Message}"); } return (cadLists.Count > 0); } private static bool getZWCadList(ref List cadLists) { string regPath = @"software\ZWSOFT\ZWCAD"; try { using (RegistryKey keyCad = Registry.LocalMachine.OpenSubKey(regPath)) { if (keyCad == null) return false; foreach (var version in keyCad.GetSubKeyNames()) { using (RegistryKey keyVersion = keyCad.OpenSubKey(version)) { if (keyVersion == null) continue; foreach (var lang in keyVersion.GetSubKeyNames()) { using (RegistryKey keyLang = keyVersion.OpenSubKey(lang)) { if (keyLang == null) continue; var location = keyLang.GetValue("Location"); // 判断注册表项是否有效 if (location == null) continue; if (Directory.GetFiles(location.ToString(), "zwcad.exe").Length == 0) continue; CADVerInfo info = new CADVerInfo(); info.setVersion(version, CadType.eCadType_ZWCAD); info.m_sMainRegKey = $"{version}\\{lang}"; info.m_sSubRegKey = lang; info.m_sCADLocation = location.ToString(); info.m_sCADName = $"中望CAD-{version}({lang})"; cadLists.Add(info); // 一个版本只有一个key是有效的,不需要继续遍历了 break; } } } } } } catch (Exception ex) { MessageBox.Show($"无法访问: {ex.Message}"); } return (cadLists.Count > 0); } public static bool getCadList(ref List cadLists, CadType type, int min, int max) { List lst = new List(); if (type == CadType.eCadType_AutoCAD) getAutoCadList(ref lst); else if (type == CadType.eCadType_ZWCAD) getZWCadList(ref lst); cadLists = lst.FindAll(x => x.m_iMainVer >= min && x.m_iMainVer <= max); if (cadLists.Count == 0) return false; return true; } public static bool getCadList(ref List cadList, List versions) { List lstACAD = new List(); List lstZWCAD = new List(); getAutoCadList(ref lstACAD); getZWCadList(ref lstZWCAD); lstACAD.AddRange(lstZWCAD); cadList = lstACAD.FindAll(x => versions.Contains(x.m_sVerStr)); if (cadList.Count == 0) return false; return true; } }; }