小众知识

C#实现搜索出本机安装的AutoCAD并实现.net程序
2016-09-02 10:56:22   来源:   评论:0 点击:

How can i get all installed versions of autoCAD ?That information should be easily obtainable by querying the registry E g Look under HMLM Software Autodesk AutoCAD

  之前已经收集了CAD各版本在注册表中的位置,见:http://hi.baidu.com/kakanimo/blog/item/36e9abee02e4e211fcfa3c53.html,那搜索出本机上安装的AutoCAD版本只需要查看注册表信息就可以了,VB方法见:http://hi.baidu.com/kakanimo/blog/item/22f6fed2185887d8a9ec9ac8.html

    写完发现C#实现起来方便多了。如下:由于.net从04开始,我们单位用的一般是06,所以从06开始搜索了。

namespace 安装.net程序
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        CheckBox[] myCheckBox = new CheckBox[10];
        string[] LocationString = new string[10];
        private void Form1_Load(object sender, EventArgs e)
        { 
            myCheckBox[0] = checkBox0;
            myCheckBox[1] = checkBox1;
            myCheckBox[2] = checkBox2;
            myCheckBox[3] = checkBox3;
            myCheckBox[4] = checkBox4;
            myCheckBox[5] = checkBox5;
            myCheckBox[6] = checkBox6;
            myCheckBox[7] = checkBox7;
            myCheckBox[8] = checkBox8;
            myCheckBox[9] = checkBox9;

           // string[] LocationString = new string[10];
            LocationString[0] = "SOFTWARE\\Autodesk\\AutoCAD\\R16.2\\ACAD-4001:804"; //2006中文版
            LocationString[1] = "SOFTWARE\\Autodesk\\AutoCAD\\R16.2\\ACAD-4001:409";//2006英文版
            LocationString[2] = "SOFTWARE\\Autodesk\\AutoCAD\\R17.0\\ACAD-5001:804";//2007中文版
            LocationString[3] = "SOFTWARE\\Autodesk\\AutoCAD\\R17.0\\ACAD-5001:409";//2007英文版
            LocationString[4] = "SOFTWARE\\Autodesk\\AutoCAD\\R17.1\\ACAD-6001:804";//2008中文版
            LocationString[5] = "SOFTWARE\\Autodesk\\AutoCAD\\R17.1\\ACAD-6001:409";//2008英文版
            LocationString[6] = "SOFTWARE\\Autodesk\\AutoCAD\\R17.2\\ACAD-7001:804";//2009中文版
            LocationString[7] = "SOFTWARE\\Autodesk\\AutoCAD\\R17.2\\ACAD-7001:409";//2009英文版
            LocationString[8] = "SOFTWARE\\Autodesk\\AutoCAD\\R18.0\\ACAD-8001:804";//2010中文版
            LocationString[9] = "SOFTWARE\\Autodesk\\AutoCAD\\R18.0\\ACAD-8001:409";//2010英文版

            for (int i = 0; i < 10; i++)
            {
                //得到电脑中安装的版本
                myCheckBox[i].Enabled = IsRegeditItemExist(LocationString[i], "AcadLocation");
            }


           // MyArx kaka = new MyArx();
            //kaka.RegApp();
            
        }
        /// <summary>
        /// 判断路径为keypath,键名为keyname的是否存在 
        /// </summary>
        /// <param name="keypath">读取路径</param>
        /// <param name="keyname">键名</param>
        /// <returns>判断keyname是否存在</returns>
        private bool IsRegeditItemExist(string keypath, string keyname) 
        {
            try
            {
                RegistryKey key = Registry.LocalMachine.OpenSubKey(keypath);//不为null表示有这个项目
                string keyval = key.GetValue(keyname).ToString();//不为null表示有这个键值
                return true;
            }
            catch
            {
                return false;
            }
        }
        //注册程序 
        public void RegApp(string keypath, string Location)
        {
            RegistryKey appkey = Registry.LocalMachine.OpenSubKey(keypath + "\\Applications", true);//打开Applications
            RegistryKey rk = appkey.CreateSubKey("KAKANIMOTools");
            rk.SetValue("DESCRIPTION", "初始化.NET程序", RegistryValueKind.String);
            rk.SetValue("LOADCTRLS", 2, RegistryValueKind.DWord);
            rk.SetValue("LOADER", Location, RegistryValueKind.String);
            rk.SetValue("MANAGED", 1, RegistryValueKind.DWord);
            appkey.Close();
        }

        public static void UnRegApp(string keypath)//卸载
        {
            RegistryKey appkey = Registry.LocalMachine.OpenSubKey(keypath + "\\Applications", true);//打开Applications
            appkey.DeleteSubKeyTree("KAKANIMOTools");

        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool isOK=false;
            for (int i = 0; i < 10; i++)
            {
                if (myCheckBox[i].Enabled == true && myCheckBox[i].Checked==true)
                {
                    //注册
                     RegApp(LocationString[i], "H:\\ChangeFonts.dll");
                     isOK = true;
                }
            }
            if (isOK == true)
            {
                MessageBox.Show("安装成功");
            }
            else
            {
                MessageBox.Show("安装失败");
            }
        }
        
    }    
}

界面如图

安装后的效果如图

 
如上,我安装到06版本后就在注册表中相应的位置HKEY_LOCAL_MACHINE\SOFTWARE\Autodesk\AutoCAD\R16.2\ACAD-4001:804\Applications\KAKANIMOTools中写入.NET DLL的信息。具体意思在网上搜索,这样设置后启动CAD时会自动加载这个DLL

相关热词搜索:C AutoCAD

上一篇:ARX函数
下一篇:最后一页

分享到: 收藏
评论排行
最新发布