C # Check whether Excel and multi-version installation are installed on the local machine? Obtain Excel process information and open the Excel application

Source: Internet
Author: User

Http://hi.baidu.com/yebihaigsino/blog/item/36e4ea6f864743d281cb4ad9.html

Http://hi.baidu.com/devzhao/blog/item/3f527e435e91de149313c6b7.html

Method 1:

Exception detection method (based on the returned result form)

// Usage
Private void buttonok_click (Object sender, eventargs E)
{
If (codeboolisexcelinstalled ())
{
MessageBox. Show ("Excel files have been installed on the local machine ");
}
Else
{
MessageBox. Show ("No Excel files can be executed in the current system. To use the EXCEL function, install Office 2003", "warning", messageboxbuttons. OK, messageboxicon. Warning );
}
}

// Determine whether an Excel file is installed on the local machine
Private bool codeboolisexcelinstalled ()
{
Type type = type. gettypefromprogid ("Excel. application ");
Return type! = NULL;
}

========================================================== ======================================

Method 2:

Registry check

Check whether the registry contains SOFTWARE \ Microsoft \ Office \ 12.0 \ word \ installroot \ excel.exe, where 12.0 11.0 must be determined at the same time, because 11.0 is Office 2003 12.0 and Office 2007

// Usage
Private void buttonok_click (Object sender, eventargs E)
{
If (existsregedit ())
{
MessageBox. Show ("Excel files have been installed on the local machine ");
}
Else
{
MessageBox. Show ("No Excel files can be executed in the current system. To use the EXCEL function, install Office 2003", "warning", messageboxbuttons. OK, messageboxicon. Warning );
}
}

/// <Summary>
/// Self_variable: queries whether a key value in the registry exists.
/// </Summary>
/// <Returns> </returns>
Public bool existsregedit ()
{
Bool ifused = false;
Registrykey rk = registry. localmachine;
Registrykey akey = rk. opensubkey (@ "Software \ Microsoft \ Office \ 11.0 \ word \ installroot \\");
Registrykey akeytwo = rk. opensubkey (@ "Software \ Microsoft \ Office \ 12.0 \ word \ installroot \\");
// Check whether office2003 is installed on the local machine
If (akey! = NULL)
{
String file03 = akey. getvalue ("path"). tostring ();
If (file. exists (file03 + "excel.exe "))
{
Ifused = true;
}
}
// Check whether the local machine has office2007 installed
If (akeytwo! = NULL)
{
String file07 = akeytwo. getvalue ("path"). tostring ();
If (file. exists (file07 + "excel.exe "))
{
Ifused = true;
}
}
Return ifused;
}

========================================================

String strinstallpath = file03/file07 + "excel.exe ";

// Obtain the Excel Process Information
System. Diagnostics. processstartinfo psinfo = new system. Diagnostics. processstartinfo (strinstallpath );

// Run Excel
System. Diagnostics. process. Start (strinstallpath );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.