How can I check that PowerPoint or point viewer is installed on the machine?

痞子三分冷 提交于 2021-02-07 09:27:23

问题


I need to play PowerPoint slides but first I want to check whether PowerPoint or viewer is installed on the machine or not. How can I do that using .NET?


回答1:


It depends on whether you are trying to tell whether you can view a presentation (*.ppt, *.pptx, etc) or whether you can access the PowerPoint object model.

To check whether there is an associated handler for ppt files, you can do the following:

// using Microsoft.Win32;
private bool CheckPowerPointAssociation() {
    var key = Registry.ClassesRoot.OpenSubKey(".ppt", false);
    if (key != null) {
        key.Close();
        return true;
    }
    else {
        return false;
    }
}

if (CheckPowerPointAssociation()) {
    Process.Start(pathToPPT);
}

To check whether the PowerPoint COM object model is available, you can check the following registry key.

// using Microsoft.Win32;
private bool CheckPowerPointAutomation() {
    var key = Registry.ClassesRoot.OpenSubKey("PowerPoint.Application", false);
    if (key != null) {
        key.Close();
        return true;
    }
    else {
        return false;
    }
}

if (CheckPowerPointAutomation()) {
    var powerPointApp = new Microsoft.Office.Interop.PowerPoint.Application();
    ....
}

Note, however, that in both cases it will only give you a pretty good indication of the availability of PowerPoint. For example, an uninstallation may not have fully removed all traces. Also in my experience selling an Outlook addin for years I've seen certain antivirus programs that interfere with the COM object model in a screwup effort to protect against malicious scripts. So in any case, have robust error handling as well.

Hope this helps!




回答2:


HKEY_CLASSES_ROOT\MSPowerPoint\protocol\StdFileEditing\server

This key is the same for all installs of PowerPoint and points to the install dir for the executable to run PowerPoint. Great to use when detecting if this product is installed and good for figuring out which folder Office products are installed in, when the install is not using the defaults.




回答3:


I am not sure this is the right way to do this. But you can use this

try
{
    //It will throw a WIN32 Exception if there is no associated 
    //application available to open the file.
    Process p = Process.Start("C:\\Sample.pptx");
}
catch (Win32Exception ex)
{
    MessageBox.Show("Powerpoint or Powerpoint viewer not installed\n");
}



回答4:


What about checking if the EXE file for PowerPoint or PowerPoint viewer exists or not by using "Exists Method" from system.io namespace?

Check this.



来源:https://stackoverflow.com/questions/1991146/how-can-i-check-that-powerpoint-or-point-viewer-is-installed-on-the-machine

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!