Wince实现软件开机自启动

时间:2024-03-18 09:39:42

Wince实现软件开机自启动的方式很多,下面列出我常用的两种。

1.修改注册表

  修改注册表[HKEY_LOCAL_MACHINE\init] 的Launch50键值为当前软件路径,不启动exploerer.exe。这种实现方式可以直接打开软件不进入桌面,但是软件一旦关闭就会死机。

这种方式比较适合产品成熟后使用,开发测试过程中不建议使用。

 string keyName;
            string valueName;

            keyName = "HKEY_LOCAL_MACHINE\\init";
            valueName = "Launch50";
            object value = null;
            string ret = null;

            try
            {
                // 获取对应键的值
                ret = (string)Registry.GetValue(keyName, valueName, value);
            }
            catch (System.ArgumentNullException)
            {
                MessageBox.Show("value 为 null");
            }
            catch (System.ArgumentException)
            {
                MessageBox.Show("keyName 未以有效注册表根开头。- 或 -valueName 的长度超过了允许的最大长度(255 个字符)。");
            }
            catch (System.UnauthorizedAccessException)
            {
                MessageBox.Show(" Microsoft.Win32.RegistryKey 是只读的,因此无法对其写入;例如,它是根级节点。");
            }
            catch (System.Security.SecurityException)
            {
                MessageBox.Show(" 用户没有创建或修改注册表项所需的权限。");
            }


          
            //得到当前目录

            string ttValue = Form1.dirname + "\\ICM.exe";

            Registry.SetValue(keyName, valueName, (object)ttValue);

            ret = (string)Registry.GetValue(keyName, valueName, value);
         

2.添加快捷方式到启动目录

  生成当前软件的快捷方式到启动目录。这种实现方式会先进入桌面再打开软件,解决了上面方案软件关闭死机的问题,但因为是进入桌面再打开呈现效果显得不够专业2333。

         string programPath = Form1.dirname + "\\ICM.exe";//程序当前路径
            string lnkPath = @"/Windows/启动/ICM.exe.lnk";//快捷方式存放到启动文件夹
            if (!File.Exists(lnkPath))
            {
                try
                {

                    string text = (programPath.Length + 2).ToString() + "#\"" + programPath + "\"";
                    byte[] buf = new byte[text.Length];
                    buf = System.Text.Encoding.GetEncoding(936).GetBytes(text.Substring(0, text.Length));
                    FileStream fs = new FileStream(lnkPath, FileMode.CreateNew);
                    fs.Write(buf, 0, buf.Length);
                    fs.Close();
                    fs = null;

                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }