StableVersion4.3/HL_FristAidPlatform_Frame/RegFile.cs

84 lines
2.8 KiB
C#
Raw Normal View History

2024-03-11 09:47:34 +08:00
using System;
using System.Diagnostics;
using System.IO;
using System.Security.Principal;
using System.Windows.Forms;
namespace HL_FristAidPlatform_Frame
{
public class RegFile
{
/// <summary>
/// 注册DLL
/// </summary>
/// <returns></returns>
public static bool RegisterDll()
{
bool result = true;
try
{
string dllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "VCodecs.dll");//获得要注册的dll的物理路径
if (!File.Exists(dllPath))
{
return false;
}
//拼接命令参数
string startArgs = string.Format("/s \"{0}\"", dllPath);
Process p = new Process();//创建一个新进程,以执行注册动作
p.StartInfo.FileName = "regsvr32";
p.StartInfo.Arguments = startArgs;
//以管理员权限注册dll文件
WindowsIdentity winIdentity = WindowsIdentity.GetCurrent(); //引用命名空间 System.Security.Principal
WindowsPrincipal winPrincipal = new WindowsPrincipal(winIdentity);
if (!winPrincipal.IsInRole(WindowsBuiltInRole.Administrator))
{
p.StartInfo.Verb = "runas";//管理员权限运行
}
p.Start();
p.WaitForExit();
p.Close();
p.Dispose();
}
catch (Exception ex)
{
MessageBox.Show("注册DLL失败" + ex);
result = false;         //记录日志,抛出异常
}
return result;
}
/// <summary>
/// 注册DLL
/// </summary>
/// <param name="strCmd"></param>
/// <returns></returns>
public static string AutoRegCom(string strCmd)
{
string rInfo;
try
{
Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("cmd.exe");
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.CreateNoWindow = true;
myProcessStartInfo.RedirectStandardOutput = true;
myProcess.StartInfo = myProcessStartInfo;
myProcessStartInfo.Arguments = "/c " + strCmd;
myProcess.Start();
StreamReader myStreamReader = myProcess.StandardOutput;
rInfo = myStreamReader.ReadToEnd();
myProcess.Close();
rInfo = strCmd + "\r\n" + rInfo;
return rInfo;
}
catch (Exception ex)
{
return ex.Message;
}
}
}
}