Source

main / Source / Excel / Excel.Common / Helper.cs

Full commit
Naethra D ba1cb57 






















































































































using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Win32;

namespace DataUp.Excel.Common
{
    /// <summary>
    /// Helper class.
    /// </summary>
    public static class Helper
    {
        /// <summary>
        /// This method reads the registry to obtain the application installation location.
        /// </summary>
        /// <returns>String - app installation location.</returns>
        public static string GetAppInstallationLocation()
        {
            string installationLocation = string.Empty;
            try
            {
                // Get the Registry key specific to the Local Machine. 
                RegistryKey regKey;
                using (var regKeyHkCU = Registry.CurrentUser)
                {
                    regKey = regKeyHkCU.OpenSubKey(Common.Constants.AppRegistryPath);
                }

                // Access the key which gives the installation location.
                if (regKey != null && regKey.GetValue(Common.Constants.AppRegistryKey) != null)
                {
                    var manifestLocation = regKey.GetValue(Common.Constants.AppRegistryKey).ToString();
                    if (!string.IsNullOrEmpty(manifestLocation))
                    {
                        if (manifestLocation.Contains("|"))
                        {
                            manifestLocation = manifestLocation.Substring(0, manifestLocation.IndexOf("|", StringComparison.OrdinalIgnoreCase));
                        }
                        installationLocation = Path.GetDirectoryName(manifestLocation);

                        installationLocation = installationLocation.StartsWith("file:\\") ? installationLocation.Remove(0, 6) : installationLocation;
                    }
                }
            }
            catch (System.Security.SecurityException ex)
            {
                Logger.LogException(ex);
            }
            catch (ArgumentException ex)
            {
                Logger.LogException(ex);
            }
            catch (UnauthorizedAccessException ex)
            {
                Logger.LogException(ex);
            }
            catch (IOException ex)
            {
                Logger.LogException(ex);
            }

            return installationLocation;
        }

        /// <summary>
        /// This method gets the list standard units supported.
        /// </summary>
        /// <returns>List of standard units.</returns>
        public static List<string> GetStandardUnits()
        {
            List<string> units = new List<string>() { };

            try
            {
                string standardUnitsPath = Common.Constants.StandardUnitsPath;
                string[] metadataValue;

                if (File.Exists(standardUnitsPath))
                {
                    metadataValue = File.ReadAllLines(standardUnitsPath);
                }
                else
                {
                    string filepath = Helper.GetAppInstallationLocation();

                    metadataValue = File.ReadAllLines(Path.Combine(filepath, "StandardUnits.txt"));
                }
                units = metadataValue.Select(p => p.Trim().ToLower()).Distinct().OrderBy(p => p).ToList();
            }
            catch
            {
                // Ignore.
            }

            return units;
        }

        /// <summary>
        /// Gets the alphabet which represents the given column number.
        /// </summary>
        /// <param name="columnNumber">Index of the column.</param>
        /// <returns>Representing alphabet</returns>
        public static string GetExcelColumnName(int columnNumber)
        {
            int dividend = columnNumber;
            string columnName = String.Empty;
            int modulo;

            while (dividend > 0)
            {
                modulo = (dividend - 1) % 26;
                columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
                dividend = (int)((dividend - modulo) / 26);
            }

            return columnName;
        }
    }
}