StableVersion4.3/HL_FristAidPlatform_Frame/RegFile.cs

84 lines
2.8 KiB
C#
Raw Blame History

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

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;
}
}
}
}