@Frank Zhu , Welcome to Microsoft Q&A,
First, we could get the build version from the Excel.exe in the installed path of computer.
// If your excel is 32 bit, please use ProgramFiles(x86)
string path1=Environment.GetEnvironmentVariable("ProgramFiles(x86)")+ "\\Microsoft Office\\Office16\\EXCEL.EXE";
// If your excel is 64 bit, please use ProgramFiles
string path2= Environment.GetEnvironmentVariable("ProgramFiles") + "\\Microsoft Office\\Office16\\EXCEL.EXE";
var file = FileVersionInfo.GetVersionInfo(path1);
Console.WriteLine(file.FileVersion);
Note: MSO version is not related to excel version, which is related to some office installation. So, there is no need to get it/
Second, you could try to install nuget-package Microsoft.Office.Interop.Excel and use the following code to check if the Excel Version is 2016 or 2021.
Excel.Application objApp = new Excel.Application();
int version=Convert.ToInt32(objApp.Version);
if(version==16)
{
Console.WriteLine("This is Excel 2016");
}
if(version==21)
{
Console.WriteLine("This is Excel 2021");
}
Tested result:
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.