注册表操作

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Diagnostics;
using System.IO;

namespace RAR压缩
{
class Program
{
static void Main(string[] args)
{
bool ss = RAR("E:\\TestDemo\\DCWriterTask", "E:\\TestDemo\\哈哈", "hao");
}

/// <summary>
/// 利用 WinRAR 进行压缩
/// </summary>
/// <param name="path">将要被压缩的文件夹(绝对路径)</param>
/// <param name="rarPath">压缩后的 .rar 的存放目录(绝对路径)</param>
/// <param name="rarName">压缩文件的名称(包括后缀)</param>
/// <returns>true 或 false。压缩成功返回 true,反之,false。</returns>
public static bool RAR(string path, string rarPath, string rarName)
{
bool flag = false;
string rarexe; //WinRAR.exe 的完整路径
RegistryKey regkey; //注册表键
Object regvalue; //键值
string cmd; //WinRAR 命令参数
ProcessStartInfo startinfo;
Process process;
try
{
string xx = Regq("HaoZip");
regkey = Registry.ClassesRoot.OpenSubKey(@"HKEY_LOCAL_MACHINE\SOFTWARE\Classes\HaoZip.001\shell\open\command");
regvalue = regkey.GetValue(""); // 键值为 "d:\Program Files\WinRAR\WinRAR.exe" "%1"
rarexe = regvalue.ToString();
regkey.Close();
rarexe = rarexe.Substring(1, rarexe.Length - 7); // d:\Program Files\WinRAR\WinRAR.exe
Directory.CreateDirectory(path);
//压缩命令,相当于在要压缩的文件夹(path)上点右键 ->WinRAR->添加到压缩文件->输入压缩文件名(rarName)
cmd = string.Format("a {0} {1} -r", rarName, path);
startinfo = new ProcessStartInfo();
startinfo.FileName = rarexe;
startinfo.Arguments = cmd; //设置命令参数
startinfo.WindowStyle = ProcessWindowStyle.Hidden; //隐藏 WinRAR 窗口
startinfo.WorkingDirectory = rarPath;
process = new Process();
process.StartInfo = startinfo;
process.Start();
process.WaitForExit(); //无限期等待进程 winrar.exe 退出
if (process.HasExited)
{
flag = true;
}
process.Close();
}
catch (Exception e)
{
throw e;
}
return flag;
}
/// <summary>
/// 利用 WinRAR 进行解压缩
/// </summary>
/// <param name="path">文件解压路径(绝对)</param>
/// <param name="rarPath">将要解压缩的 .rar 文件的存放目录(绝对路径)</param>
/// <param name="rarName">将要解压缩的 .rar 文件名(包括后缀)</param>
/// <returns>true 或 false。解压缩成功返回 true,反之,false。</returns>
public static bool UnRAR(string path, string rarPath, string rarName)
{
bool flag = false;
string rarexe;
RegistryKey regkey;
Object regvalue;
string cmd;
ProcessStartInfo startinfo;
Process process;
try
{
regkey = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\shell\open\command");
regvalue = regkey.GetValue("");
rarexe = regvalue.ToString();
regkey.Close();
rarexe = rarexe.Substring(1, rarexe.Length - 7);
Directory.CreateDirectory(path);
//解压缩命令,相当于在要压缩文件(rarName)上点右键 ->WinRAR->解压到当前文件夹
cmd = string.Format("x {0} {1} -y", rarName, path);
startinfo = new ProcessStartInfo();
startinfo.FileName = rarexe;
startinfo.Arguments = cmd;
startinfo.WindowStyle = ProcessWindowStyle.Hidden;
startinfo.WorkingDirectory = rarPath;
process = new Process();
process.StartInfo = startinfo;
process.Start();
process.WaitForExit();
if (process.HasExited)
{
flag = true;
}
process.Close();
}
catch (Exception e)
{
throw e;
}
return flag;
}

public static string Reg(string access)
{
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall", false))
{
if (key != null)//判断对象存在
{
foreach (string keyName in key.GetSubKeyNames())//遍历子项名称的字符串数组
{
using (RegistryKey key2 = key.OpenSubKey(keyName, false))//遍历子项节点
{
if (key2 != null)
{
string softwareName = key2.GetValue("DisplayName", "").ToString();//获取软件名
if (softwareName.IndexOf(access) > -1)
{
string installLocation = key2.GetValue("InstallLocation", "").ToString();//获取安装路径
//return true;
}
}
}
}
}
}
//return false;
return null;
}
public static string Regq(string exe)
{
string strKeyName = ""; //"(Default)" key, which contains the intalled path
string objResult = null;
RegistryKey regSubKey = null;

using (regSubKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\" + exe + ".exe", false))
{ //Read the path
if (regSubKey != null)
{
objResult = Convert.ToString(regSubKey.GetValue(strKeyName));
return objResult;
}
}
return null;
}
}
}

posted @ 2016-03-18 16:32  龙骑科技  阅读(191)  评论(0编辑  收藏  举报