2014-12-07 1 views
1

Я должен определить местоположение установки excel в реестре и открыть файл excel. Я нашел пример кода. У меня есть две проблемы, во-первых нет InstallRoot directorty в данном пути реестра в этом кодеconfusion excel установить место в реестре

RegistryKey RootDir = excelKey.OpenSubKey (CurrentVersion + @ "0 \ Excel \ InstallRoot.");

Но он существует в рамках этого пути (SOFTWARE \ Wow6432Node \ Microsoft \ Office) Другая проблема, которую я получал сообщение, которое говорит, что «не может открыть в Excel , потому что первенствует не установлен.» Таким образом, Я думал, что CurrentVersion возвращает значение worng в моем случае (с 2007 офиса) возвращает 120,0 вместо 12,0

public void OpenInExcel(string filename) 
     { 
      string dir = ""; 
      RegistryKey key = Registry.LocalMachine; 
      RegistryKey excelKey = key.OpenSubKey(@"SOFTWARE\MicroSoft\Office"); 
      if (excelKey != null) 
      { 
       foreach (string valuename in excelKey.GetSubKeyNames()) 
       { 
        int version = 9; 
        double currentVersion=0; 
        if (Double.TryParse(valuename, out currentVersion) && currentVersion >= version) 
        { 
         RegistryKey rootdir = excelKey.OpenSubKey(currentVersion + @".0\Excel\InstallRoot"); 
         if (rootdir != null) 
         { 
          dir = rootdir.GetValue(rootdir.GetValueNames()[0]).ToString(); 
          break; 
         } 
        } 
       }     
      } 
      if (dir != "") 
      { 
       ProcessStartInfo startInfo = new ProcessStartInfo(); 

       startInfo.FileName = dir + @"Excel.exe"; 
       startInfo.Arguments = "\"" + fileName + "\""; 
       startInfo.UseShellExecute = false; 

       using (Process process = new Process()) 
       { 
        process.StartInfo = startInfo; 
        try 
        { 
         process.Start(); 
        } 
        catch (Exception ex) 
        { 
         Console.WriteLine("\n\nCould not start Excel process."); 
         Console.WriteLine(ex); 
        } 
       } 
      } 
      else 
      { 
       MessageBox.Show("Can't Open in excel because excel is not installed."); 
      } 
     }  

@Edit: Я думаю, что что-то пойдет не так в этой строке, которая возвращает неверное значение, потому что это работает таким образом

RegistryKey rootdir = excelKey.OpenSubKey ( @ "12.0 \ Excel \ InstallRoot");

Я не знаю, почему TryParse возвращает 120,0

+1

Есть ли конкретная причина, почему вы не хотите, чтобы установить 'startInfo.UseShellExecute = true', 'startInfo.FileName = @" document.xls "и позволяет ОС делать все остальное? – GSerg

+0

Вы отметили ответ RenniePet. 'Это не предназначено для ответа на ваш вопрос' – Kiquenet

ответ

0

Это не предназначено, чтобы быть ответом на ваш вопрос, но это может помочь вам получить правильный номер версии, и я не могу отправить весь этот код в комментарии.

public const string CProgIdOutlook = "Outlook.Application"; 

    /// <summary> 
    /// Method to get the Outlook version number, which will presumably be 11 (2003), 12 (2007), 
    /// 14 (2010) or 15 (2013). (I wonder what happened to version 13?) An exception is thrown if 
    /// this method is unable to provide an answer. 
    /// 
    /// The technique used to get the version number of the "current" installed version of Outlook 
    /// is one of many possible methods that are described in various Internet sources, and is 
    /// hopefully the most likely to provide the correct answer with the least side-effects. 
    /// Problems with some of the alternative registry-based methods typically show up when 
    /// multiple versions of Outlook are installed or have been installed. The registry values can 
    /// also depend on x86 vs. x64 systems and whether Outlook was installed for one user or for 
    /// all users. Techniques involving querying the Outlook automation object have the 
    /// disadvantage of an instance of the Outlook program getting created - this can be seen in 
    /// Task Manager. 
    /// 
    /// The idea for this code came from here: http://support.microsoft.com/kb/240794 
    /// </summary> 
    /// <returns>11 (2003), 12 (2007), 14 (2010) or 15 (2013)</returns> 
    private static int GetOutlookVersion() 
    { 
    const string CRegistryKey = @"SOFTWARE\Classes\" + GroupwareProgIds.CProgIdOutlook; 
    int outlookVersion; 

    using (RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(CRegistryKey)) 
    { 
     outlookVersion = GetOutlookVersion(registryKey); 
     if (outlookVersion != -1) 
      return outlookVersion; 
    } 

    using (RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(CRegistryKey)) 
    { 
     outlookVersion = GetOutlookVersion(registryKey); 
     if (outlookVersion != -1) 
      return outlookVersion; 
    } 

    throw new MerliniaException(0x2d4a67fu, "No registry entry for " + CRegistryKey); 
    } 


    /// <summary> 
    /// Sub-method of above method to do the work for either HKLM or HKCU. 
    /// </summary> 
    /// <returns>11 (2003), 12 (2007), 14 (2010) or 15 (2013), or -1 for error</returns> 
    private static int GetOutlookVersion(RegistryKey registryKey1) 
    { 
    const string CCurVer = "CurVer"; 

    if (registryKey1 == null) 
     return -1; 

    using (RegistryKey registryKey2 = registryKey1.OpenSubKey(CCurVer)) 
    { 
     if (registryKey2 == null) 
      throw new MerliniaException(0x2d43e5au, 
             "No registry entry for " + registryKey1 + "\\" + CCurVer); 

     string currentOutlookAppName = registryKey2.GetValue(null) as string; 
     if (currentOutlookAppName != null) 
     { 
      string[] sa = currentOutlookAppName.Split('.'); 
      if (sa.Length == 3) 
      { 
       int outlookVersion; 
       if (int.TryParse(sa[2], NumberStyles.Integer, 
           CultureInfo.InvariantCulture, out outlookVersion)) 
       return outlookVersion; 
      } 
     } 

     throw new MerliniaException(0x2d4b29du, 
           "Invalid registry content for " + registryKey1 + "\\" + CCurVer); 
    } 
    } 
0

Проблема составляет около типа двойного и Культура.

Я пытаюсь объяснить:

Вы получаете двойного значения12.0.

И ToString новообращенный 12,0 в Культура ан-США и конвертировать в 120,0 в Культура эс-ES (десятичные разделители не то же самое).

Проблема была разбор версии как двойной.

Поскольку компьютерная культура рассматривает как десятичный разделитель и. как разделитель тысяч

Вы можете изменяет код, как это (когда вы пытались Double.TryParse):

if (Double.TryParse(valuename, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out currentVersion) && currentVersion >= version)

Смежные вопросы