views:

181

answers:

2

How can I achieve that in .NET/C#?

+1  A: 

Read your app.config file as it will place the reference in there:

<compilation debug="false">
    <assemblies>
        <add assembly="Office, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"/>
    </assemblies>
</compilation>

By "Read", you may have to open the file and read the contents as I don't think that you can specifically read Assemblies.

Ardman
Instead of reading the app.config file you could use reflection name space with Assembly myAss = Assembly.GetAssembly(type) where type is a type of object from the Excel assembly. The FullPath property would return the same information as is stored in the app.config and you could just parse that.
Jay
Using reflection I could determine the Excel version and culture but I still need to know the Office/Excel installation language. How can I discover that?
DotNetter
A: 

You could use this code snippet: (taken from one of my projects, so not guaranteed to work out of the box)

            Microsoft.Office.Interop.Excel.Application tExcel = new Application();
            CultureInfo cSystemCulture = Thread.CurrentThread.CurrentCulture;
            CultureInfo cExcelCulture = new CultureInfo(tExcel.LanguageSettings.get_LanguageID(
                Microsoft.Office.Core.MsoAppLanguageID.msoLanguageIDUI));

            try
            {
                Thread.CurrentThread.CurrentCulture = cExcelCulture;
                double tVersion;
                bool tParseSucceded = double.TryParse(tExcel.Version, out tVersion);

                // 12 is the first version with .xlsx extension
                if (tVersion > 11.5)
                    cDefaultExtension = ".xlsx";
                else
                    cDefaultExtension = ".xls";

            }
            catch (Exception aException)
            {
                cLogger.Debug("error retrieving excel version.", aException);
                cLogger.Error("error retrieving excel version.");
            }
            finally
            {
                Thread.CurrentThread.CurrentCulture = cSystemCulture;
            }
yas4891