Beispiel #1
0
        /// <summary>
        /// 设置开机自启动
        /// </summary>
        /// <param name="filePath">程序路径</param>
        /// <param name="isAutoRun">true开机启动/false删除开机启动</param>
        public static void SetAutoRun(string filePath, bool isAutoRun)
        {
            RegistryKey localMachineRegistry = RegistryKeyEx.OpeBaseKey(RegistryHive.LocalMachine);
            RegistryKey runRegKey            = null;

            try
            {
                if (!System.IO.File.Exists(filePath))
                {
                    throw new Exception(filePath + "应用程序不存在!");
                }

                string name = System.IO.Path.GetFileName(filePath);
                runRegKey = localMachineRegistry.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                if (runRegKey == null)
                {
                    runRegKey = localMachineRegistry.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                }

                //是否开机自动启动
                if (isAutoRun)
                {
                    runRegKey.SetValue(name, filePath);
                }
                else
                {
                    runRegKey.SetValue(name, false);
                }
            }
            finally
            {
                RegistryKeyEx.CloseRegistryKey(runRegKey);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 设置开机自动登录
        /// </summary>
        /// <param name="userName">系统登录用户名</param>
        /// <param name="password">系统登录密码</param>
        public static void SetAutoLogin(string userName, string password)
        {
            RegistryKey localMachineRegistry = RegistryKeyEx.OpeBaseKey(RegistryHive.LocalMachine);

            //在注册表中设置开机自动登录程序
            string      winlogonRegKeyPath = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon";
            RegistryKey autoLogOnRegKey    = null;

            try
            {
                autoLogOnRegKey = localMachineRegistry.OpenSubKey(winlogonRegKeyPath, true);
                if (autoLogOnRegKey == null)
                {
                    //如果子键节点不存在,则创建之
                    autoLogOnRegKey = Registry.LocalMachine.CreateSubKey(winlogonRegKeyPath);
                }
                //在注册表中设置自启动程序
                autoLogOnRegKey.SetValue("AutoAdminLogon", "1");
                autoLogOnRegKey.SetValue("DefaultUserName", userName);
                autoLogOnRegKey.SetValue("DefaultPassword", password);
            }
            finally
            {
                RegistryKeyEx.CloseRegistryKey(autoLogOnRegKey);
            }
        }
Beispiel #3
0
        /// <summary>
        /// 使本程序与文件类型关联
        /// </summary>
        /// <param name="fileTypeName">文件类型名</param>
        /// <param name="fileExtension">文件扩展名</param>
        /// <param name="associateAppPath">安装程序路径</param>
        public static void AssociateWithFile(string fileTypeName, string fileExtension, string associateAppPath)
        {
            if (string.IsNullOrWhiteSpace(fileTypeName))
            {
                throw new ArgumentNullException("fileTypeName");
            }

            if (string.IsNullOrWhiteSpace(fileExtension))
            {
                throw new ArgumentNullException("fileExtension");
            }

            if (string.IsNullOrWhiteSpace(associateAppPath))
            {
                throw new ArgumentNullException("associateAppPath");
            }

            // 检查fileExtension参数格式是否正确
            Regex rgx = new Regex(@"^\.?(?<extension>\w+)$");
            Match mh  = rgx.Match(fileExtension);

            if (!mh.Success)
            {
                throw new Exception("参数fileExtension格式错误");
            }

            RegistryKey classesRootKey = RegistryKeyEx.OpeBaseKey(RegistryHive.ClassesRoot);
            // 在HKEY_CLASSES_ROOT下创建名为fileTypeName的节点
            RegistryKey key = classesRootKey.CreateSubKey(fileTypeName);//临时的注册表项

            // 保存自定义节点,用于以后检查关联程序路径是否正确
            key.SetValue("Create", associateAppPath);
            // 创建关联图标节点
            RegistryKey keyico = key.CreateSubKey("DefaultIcon");//关联图标的注册表项

            // 设置关联图标路径
            keyico.SetValue(string.Empty, associateAppPath + ",1");
            // 设置关联文件描述
            key.SetValue(string.Empty, fileTypeName);
            key = key.CreateSubKey("Shell");
            key = key.CreateSubKey("Open");
            key = key.CreateSubKey("Command");
            // 关联的程序的启动位置
            key.SetValue(string.Empty, string.Format("\"{0}\"  \"%1\"", associateAppPath));
            // 或旧的文件扩展名关联节点
            string extension = string.Format(".{0}", mh.Groups["extension"].Value);

            // 关联的文件扩展名
            key = classesRootKey.CreateSubKey(extension);
            key.SetValue(string.Empty, fileTypeName);
            classesRootKey.Close();
            key.Close();
            keyico.Close();
        }
Beispiel #4
0
        /// <summary>
        /// 分离本程序与文件类型的关联
        /// </summary>
        /// <param name="fileTypeName">文件类型名</param>
        /// <param name="fileExtension">文件扩展名</param>
        public static void DisassocateWithFile(string fileTypeName, string fileExtension)
        {
            if (string.IsNullOrWhiteSpace(fileTypeName))
            {
                throw new ArgumentNullException("fileTypeName");
            }

            if (string.IsNullOrWhiteSpace(fileExtension))
            {
                throw new ArgumentNullException("fileExtension");
            }

            // 检查fileExtension参数格式是否正确
            Regex rgx = new Regex(@"^\.?(?<extension>\w+)$");
            Match mh  = rgx.Match(fileExtension);

            if (!mh.Success)
            {
                throw new Exception("参数fileExtension格式错误");
            }

            // 获取文件扩展名(有点号)
            string      extension      = string.Format(".{0}", mh.Groups["extension"].Value);
            RegistryKey classesRootKey = RegistryKeyEx.OpeBaseKey(RegistryHive.ClassesRoot);
            // 获取文件关联注册表项
            RegistryKey fileAssociationKey = classesRootKey.OpenSubKey(extension);

            // 如果注册表项关联类型为fileTypeName则,删除这项
            if (null != fileAssociationKey &&
                string.Equals(fileAssociationKey.GetValue(string.Empty).ToString(), fileTypeName, StringComparison.OrdinalIgnoreCase))
            {
                Registry.ClassesRoot.DeleteSubKeyTree(extension);
                fileAssociationKey.Close();
            }

            // 获取名为fileTypeName的注册表ClassesRoot的子项fileAssociationKey
            RegistryKey fileTypeDiscriptionKey = classesRootKey.OpenSubKey(fileTypeName);

            if (fileTypeDiscriptionKey != null)
            {
                // 程序启动路径正确则表示已经关联,则删除这个注册表项
                Registry.ClassesRoot.DeleteSubKeyTree(fileTypeName);
                fileTypeDiscriptionKey.Close();
            }

            classesRootKey.Close();
        }
Beispiel #5
0
        /// <summary>
        /// 异常关机(断电重启)以后是否出现关闭事件跟踪程序
        /// </summary>
        /// <param name="value">0:不出现关闭事件跟踪程序,1:出现关闭事件跟踪程序</param>
        public static void SetNotEventTrace(int value)
        {
            RegistryKey localMachineRegistry = RegistryKeyEx.OpeBaseKey(RegistryHive.LocalMachine);

            string      reliabilityRegKeyPath = @"SOFTWARE\Policies\Microsoft\Windows NT\Reliability";
            RegistryKey exEventRegKey         = null;

            try
            {
                exEventRegKey = localMachineRegistry.OpenSubKey(reliabilityRegKeyPath, true);
                if (exEventRegKey == null)
                {
                    //如果子键节点不存在,则创建之
                    exEventRegKey = Registry.LocalMachine.CreateSubKey(reliabilityRegKeyPath);
                }
                exEventRegKey.SetValue("ShutdownReasonOn", value);
            }
            finally
            {
                RegistryKeyEx.CloseRegistryKey(exEventRegKey);
            }
        }