2025-03-28 09:49:56 +08:00

136 lines
5.3 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Win32;
using System.Diagnostics;
using System.Security.AccessControl;
namespace ConnectPoint
{
class RegistryHelper
{
/// <summary>
/// 读取指定名称的注册表的值
/// </summary>
/// <param name="root">注册表顶级节点</param>
/// <param name="subkey">注册表项所在路径</param>
/// <param name="name">注册表项名称</param>
/// <returns>注册表项值</returns>
public string GetRegistryData(RegistryKey root, string subkey, string name)
{
string registData = "";
RegistryKey myKey = root.OpenSubKey(subkey,
RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.QueryValues);
if (myKey != null)
{
registData = myKey.GetValue(name).ToString();
}
return registData;
}
/// <summary>
/// 创建或修改指定名称的注册表的值
/// </summary>
/// <param name="root">注册表顶级节点</param>
/// <param name="subkey">注册表项所在路径</param>
/// <param name="name">注册表项名称</param>
/// <param name="value">注册表项值</param>
/// <param name="valueKind">值类型</param>
public void SetRegistryData(RegistryKey root, string subkey, string name, string value, RegistryValueKind valueKind)
{
RegistryKey aimdir = root.CreateSubKey(subkey,
RegistryKeyPermissionCheck.ReadWriteSubTree);
aimdir.SetValue(name, value, valueKind);
}
/// <summary>
/// 删除指定名称的注册表项
/// </summary>
/// <param name="root">注册表顶级节点</param>
/// <param name="subkey">注册表项所在路径</param>
/// <param name="name">注册表项名称</param>
public void DeleteRegist(RegistryKey root, string subkey, string name)
{
string[] subkeyNames;
RegistryKey myKey = root.OpenSubKey(subkey,
RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.SetValue);
subkeyNames = myKey.GetSubKeyNames();
foreach (string aimKey in subkeyNames)
{
if (aimKey == name)
myKey.DeleteSubKeyTree(name);
}
}
/// <summary>
/// 判断指定名称的注册表项是否存在
/// </summary>
/// <param name="root">注册表顶级节点</param>
/// <param name="subkey">注册表项所在路径</param>
/// <param name="name">注册表项名称</param>
/// <returns></returns>
public bool IsRegistryExist(RegistryKey root, string subkey, string name)
{
bool _exit = false;
string[] subkeyNames;
RegistryKey myKey = root.OpenSubKey(subkey,
RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.QueryValues);
subkeyNames = myKey.GetSubKeyNames();
foreach (string keyName in subkeyNames)
{
if (keyName == name)
{
_exit = true;
return _exit;
}
}
return _exit;
}
/// <summary>
/// 运行Dos命令并返回结果
/// </summary>
/// <param name="command">Dos命令</param>
/// <param name="seconds">进程等待时间(毫秒)</param>
/// <returns></returns>
public static string Execute(string command, int seconds = 2000)
{
string output = ""; //输出字符串     
if (command != null && !command.Equals(""))
{
Process process = new Process();//创建进程对象     
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";//设定需要执行的命令     
startInfo.Arguments = "/C " + command;//“/C”表示执行完命令后马上退出     
startInfo.UseShellExecute = false;//不使用系统外壳程序启动     
startInfo.RedirectStandardInput = false;//不重定向输入     
startInfo.RedirectStandardOutput = true; //重定向输出     
startInfo.CreateNoWindow = true;//不创建窗口     
process.StartInfo = startInfo;
try
{
if (process.Start())//开始进程     
{
if (seconds == 0)
{
process.WaitForExit();//这里无限等待进程结束     
}
else
{
process.WaitForExit(seconds); //等待进程结束,等待时间为指定的毫秒     
}
output = process.StandardOutput.ReadToEnd();//读取进程的输出     
}
}
catch
{
}
finally
{
if (process != null)
process.Close();
}
}
return output;
}
}
}