250 lines
9.5 KiB
C#
250 lines
9.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using Microsoft.Win32;
|
|
using System.Windows.Forms;
|
|
|
|
namespace ryCommon
|
|
{
|
|
/// <summary>
|
|
/// 设置注册表操作,部分功能需要管理员权限
|
|
/// </summary>
|
|
public class RyRegedit
|
|
{
|
|
/// <summary>
|
|
/// 访问的注册表位置(64位还是32位)
|
|
/// </summary>
|
|
public static RegistryView UseSystemBit = RegistryView.Default;
|
|
/// <summary>
|
|
/// 访问的注册表节点
|
|
/// </summary>
|
|
public static RegistryHive RegRoot = RegistryHive.LocalMachine;
|
|
/// <summary>
|
|
/// 设置是否开机启动
|
|
/// </summary>
|
|
/// <param name="AutoRun">是否开机启动</param>
|
|
/// <param name="StartName">开机启动名称</param>
|
|
/// <param name="StartCommand">启动命令</param>
|
|
/// <returns></returns>
|
|
public static bool SetAutoRun(bool AutoRun, string StartName, string StartCommand)
|
|
{
|
|
try
|
|
{
|
|
RegistryKey LMach = RegistryKey.OpenBaseKey(RegRoot, UseSystemBit);
|
|
RegistryKey softwareRun = LMach.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
|
|
if (AutoRun)
|
|
{ softwareRun.SetValue(StartName, StartCommand, RegistryValueKind.String); }
|
|
else
|
|
{
|
|
if (softwareRun.GetValue(StartName) != null)
|
|
{
|
|
softwareRun.DeleteValue(StartName);
|
|
}
|
|
}
|
|
softwareRun.Close();
|
|
LMach.Close();
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 设置是否开机启动
|
|
/// </summary>
|
|
/// <param name="AutoRun">是否开机启动</param>
|
|
/// <param name="StartName">开机启动名称</param>
|
|
/// <returns></returns>
|
|
public static bool SetAutoRun(bool AutoRun, string StartName)
|
|
{
|
|
return SetAutoRun(AutoRun, StartName, "\"" + Application.ExecutablePath + "\" q");
|
|
}
|
|
/// <summary>
|
|
/// 把指定文件设置为开机启动或取消开机启动
|
|
/// </summary>
|
|
/// <param name="AutoRun">是否开机启动</param>
|
|
/// <param name="StartName">开机启动名称</param>
|
|
/// <param name="Path">要开机启动的文件路径</param>
|
|
/// <returns></returns>
|
|
public static bool SetAutoRunByPath(bool AutoRun, string StartName, string Path)
|
|
{
|
|
return SetAutoRun(AutoRun, StartName, "\"" + Path + "\" q");
|
|
}
|
|
/// <summary>
|
|
/// 检查是否开机启动
|
|
/// </summary>
|
|
/// <param name="StartName">开机启动名称</param>
|
|
/// <param name="StartCommand">开机启动命令</param>
|
|
/// <returns></returns>
|
|
public static bool IsAutoRun(string StartName, string StartCommand)
|
|
{
|
|
try
|
|
{
|
|
bool sxResult = false;
|
|
RegistryKey LMach = RegistryKey.OpenBaseKey(RegRoot, UseSystemBit);
|
|
RegistryKey softwareRun = LMach.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
|
|
if (softwareRun.GetValue(StartName, "").ToString() == StartCommand)
|
|
{ sxResult = true; }
|
|
else { sxResult = false; }
|
|
softwareRun.Close();
|
|
LMach.Close();
|
|
return sxResult;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 检查是否开机启动
|
|
/// </summary>
|
|
/// <param name="StartName">开机启动名称</param>
|
|
/// <returns></returns>
|
|
public static bool IsAutoRun(string StartName)
|
|
{
|
|
try
|
|
{
|
|
bool sxResult = false;
|
|
RegistryKey LMach = RegistryKey.OpenBaseKey(RegRoot, UseSystemBit);
|
|
RegistryKey softwareRun = LMach.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
|
|
if (softwareRun.GetValue(StartName, "").ToString() != "")
|
|
{ sxResult = true; }
|
|
else { sxResult = false; }
|
|
softwareRun.Close();
|
|
LMach.Close();
|
|
return sxResult;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 判断指定文件是否是开机启动
|
|
/// </summary>
|
|
/// <param name="StartName">开机启动名称</param>
|
|
/// <param name="Path">文件路径</param>
|
|
/// <returns></returns>
|
|
public static bool IsAutoRunByPath(string StartName, string Path)
|
|
{
|
|
return IsAutoRun(StartName, "\"" + Path + "\" q");
|
|
}
|
|
/// <summary>
|
|
/// 判断当前程序是否是开机启动
|
|
/// </summary>
|
|
/// <param name="StartName">开机启动名称</param>
|
|
/// <returns></returns>
|
|
public static bool IsAutoRunByMe(string StartName)
|
|
{
|
|
return IsAutoRun(StartName, "\"" + Application.ExecutablePath + "\" q");
|
|
}
|
|
/// <summary>
|
|
/// 判断当前程序是否是开机启动
|
|
/// </summary>
|
|
/// <param name="StartName">开机启动名称</param>
|
|
/// <param name="Cmd">启动命令行</param>
|
|
/// <returns></returns>
|
|
public static bool IsAutoRunByMe(string StartName, string Cmd)
|
|
{
|
|
return IsAutoRun(StartName, "\"" + Application.ExecutablePath + "\" " + Cmd);
|
|
}
|
|
/// <summary>
|
|
/// 设置指定文件的浏览器控件内核版本
|
|
/// </summary>
|
|
/// <param name="filename">文件名,要求不带路径</param>
|
|
/// <param name="IEMode">7000 表示IE7兼容视图模式;8000 表示IE8 标准模式 ;8888 表示IE8 强制标准模式,在渲染失败的情况下不尝试用兼容视图模式</param>
|
|
/// <returns></returns>
|
|
public static bool SetIE_EMULATION(string filename, uint IEMode)
|
|
{
|
|
try
|
|
{
|
|
RegistryKey LMach = RegistryKey.OpenBaseKey(RegRoot, UseSystemBit);
|
|
RegistryKey softwareRun = LMach.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION", true);
|
|
softwareRun.SetValue(filename, IEMode, RegistryValueKind.DWord);
|
|
softwareRun.Close();
|
|
LMach.Close();
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 采用的IE模式
|
|
/// </summary>
|
|
public enum IeMode
|
|
{
|
|
/// <summary>
|
|
/// IE7兼容视图
|
|
/// </summary>
|
|
IE7CompatibleView = 7000,
|
|
/// <summary>
|
|
/// IE8 标准模式
|
|
/// </summary>
|
|
IE8StandardMode = 8000,
|
|
/// <summary>
|
|
/// IE8 强制标准模式,在渲染失败的情况下不尝试用兼容视图模式
|
|
/// </summary>
|
|
IE8Forced = 8888,
|
|
/// <summary>
|
|
/// IE9 标准模式
|
|
/// </summary>
|
|
IE9StandardMode = 9000,
|
|
/// <summary>
|
|
/// IE9 强制标准模式,在渲染失败的情况下不尝试用兼容视图模式
|
|
/// </summary>
|
|
IE9Forced = 9999,
|
|
/// <summary>
|
|
/// IE10 标准模式
|
|
/// </summary>
|
|
IE10StandardMode = 10000,
|
|
/// <summary>
|
|
/// IE10 强制标准模式,在渲染失败的情况下不尝试用兼容视图模式
|
|
/// </summary>
|
|
IE10Forced = 10001
|
|
}
|
|
/// <summary>
|
|
/// 设置当前软件的浏览器控件内核版本
|
|
/// </summary>
|
|
/// <param name="IEMode">7000 表示IE7兼容视图模式;8000 表示IE8 标准模式 ;8888 表示IE8 强制标准模式,在渲染失败的情况下不尝试用兼容视图模式</param>
|
|
/// <returns></returns>
|
|
public static bool SetIE_EMULATION(uint IEMode)
|
|
{
|
|
return SetIE_EMULATION(System.IO.Path.GetFileName(Application.ExecutablePath), IEMode);
|
|
}
|
|
/// <summary>
|
|
/// 设置指定文件的浏览器控件内核版本
|
|
/// </summary>
|
|
/// <param name="filename">文件名,要求不带路径</param>
|
|
/// <param name="IEMode">IE内核版本</param>
|
|
/// <returns></returns>
|
|
public static bool SetIE_EMULATION(string filename, IeMode IEMode)
|
|
{
|
|
try
|
|
{
|
|
RegistryKey LMach = RegistryKey.OpenBaseKey(RegRoot, UseSystemBit);
|
|
RegistryKey softwareRun = LMach.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION", true);
|
|
softwareRun.SetValue(filename, IEMode, RegistryValueKind.DWord);
|
|
softwareRun.Close();
|
|
LMach.Close();
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 设置当前软件的浏览器控件内核版本
|
|
/// </summary>
|
|
/// <param name="IEMode">IE内核版本</param>
|
|
/// <returns></returns>
|
|
public static bool SetIE_EMULATION(IeMode IEMode)
|
|
{
|
|
return SetIE_EMULATION(System.IO.Path.GetFileName(Application.ExecutablePath), IEMode);
|
|
}
|
|
}
|
|
}
|