public class RegisOffice
{
public static IList<OfficeVersion> GetOfficeRegistryKeyInfo()
{
IList<OfficeVersion> registriedOfficeVersionList = new List<OfficeVersion>();
RegistryKey rkCurrentUser = Registry.CurrentUser;
foreach (var officever in Enum.GetValues(typeof(OfficeVersion)))
{
if (IsRegistrationOfficeByOfficeVersion((OfficeVersion)officever))
registriedOfficeVersionList.Add((OfficeVersion)officever);
}
return registriedOfficeVersionList;
}
private static bool IsRegistrationOfficeByOfficeVersion(OfficeVersion officeVersion)
{
RegistryKey rkCurrentUser = Registry.CurrentUser;
RegistryKey rkRegistration = rkCurrentUser.OpenSubKey(string.Format(@"Software\Microsoft\Office\{0}.0\Registration\", (int)officeVersion));
return rkRegistration == null ? false : true;
}
public static void AddOfficeReliancePath()
{
try
{
string filePath = Environment.CurrentDirectory + "\\" + "your directory" + "\\";
AddOfficeReliancePath(filePath);
}
catch (Exception ex)
{
//添加信任地址失败
}
}
public static void AddOfficeReliancePath(string path)
{
IList<OfficeVersion> registriedOfficeVersionList = GetOfficeRegistryKeyInfo();
foreach (var ov in registriedOfficeVersionList)
{
AddOfficeReliancePath(ov, OfficeComponents.Word, path);
AddOfficeReliancePath(ov, OfficeComponents.PowerPoint, path);
}
}
public static void AddOfficeReliancePath(OfficeVersion officeVersion, OfficeComponents officeComponents, string path)
{
RegistryKey rkCurrentUser = Registry.CurrentUser;
RegistryKey rkLocation = rkCurrentUser.OpenSubKey(string.Format("Software\\Microsoft\\Office\\{0}.0\\{1}\\Security\\Trusted Locations", (int)officeVersion, officeComponents.ToString()), true);
RegistryKey rkLocationSmartLesson = rkLocation.CreateSubKey("LocationSmartLesson");
rkLocationSmartLesson.SetValue("Path", path);
}
}
public enum OfficeVersion
{
Office2003 = 11,
Office2007 = 12,
Office2010 = 14,
Office2013 = 15,
Office2016 = 16
}
public enum OfficeComponents
{
Word = 1,
PowerPoint =2,
Excel =3,
Access =4,
Outlook =5
}