//str 用于在cmd中执行的第一条语句,进入此文件夹,好执行regasm命令
//str1 regasm 指定目录下的dll文件,加上/codebase 表明全局注册
string str = @"cd /d C:\Windows\Microsoft.NET\Framework\v4.0.30319";
string str1 = @"regasm " + AppDomain.CurrentDomain.BaseDirectory + @"dll\UnipenCamera.dll /codebase ";
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
p.StartInfo.CreateNoWindow = true;//不显示程序窗口
p.Start();//启动程序
//向cmd窗口发送输入信息
p.StandardInput.WriteLine(str);
p.StandardInput.WriteLine(str1 + "&exit");
p.StandardInput.AutoFlush = true;
//p.StandardInput.WriteLine("exit");
//向标准输入写入要执行的命令。这里使用&是批处理命令的符号,表示前面一个命令不管是否执行成功都执行后面(exit)命令,如果不执行exit命令,后面调用ReadToEnd()方法会假死
//同类的符号还有&&和||前者表示必须前一个命令执行成功才会执行后面的命令,后者表示必须前一个命令执行失败才会执行后面的命令
//获取cmd窗口的输出信息。可用于检测是否成功
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();//等待程序执行完退出进程
p.Close();
//分割线///
///执行目录下的可执行文件 filename 可执行文件所在目录和名称 args执行语句
例如:
DriverSetup(AppDomain.CurrentDomain.BaseDirectory + @"tools\Driver install.exe", "/s /q /norestart ");
public static void DriverSetup(string filename, string args)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = filename;
psi.Arguments = args;
psi.WindowStyle = ProcessWindowStyle.Normal;
Process proc = Process.Start(psi);
proc.WaitForExit();
}
/分割线
///用于检测注册表的值
//path 注册表路径 name(flag=0时项名称,flag=1时键名称),flag 为0 判断该项指定的(name)注册表项是否存在。存在返
//回true,flag不为0则判断该项下面注册表键值对是否存在, os用于返回值(类型自定义),存在os储存该值,不存在为 0
public static bool ChekReg(string path,string name, int flag,ref double os)
{
if (flag == 0)
{
RegistryKey subKey = Registry.LocalMachine.OpenSubKey(path);
string[] keyNames = subKey.GetSubKeyNames();
subKey.Close();
bool result = false;
foreach (string keyName in keyNames)
{
if (keyName == name)
{
result = true;
break;
}
else
{
result = false;
}
}
return result;
}
else
{
string path1 = "HKEY_LOCAL_MACHINE\\" + path;
Object obj = Registry.GetValue(path1, name, null);
os = Convert.ToDouble( obj);
return true;
}