从java确定Outlook.exe的路径?

时间:2008-12-31 18:09:14

标签: java outlook

我想从命令行调用outlook(出于各种原因),并想知道如何发现Outlook.exe文件的路径。

我很确定它存储在注册表中,但是想知道如何从Java中读取它。

感谢

4 个答案:

答案 0 :(得分:1)

我发现了一个描述该过程的Microsoft page,而不是Java。

所以我想问题是如何从java访问注册表。

答案 1 :(得分:1)

我发现this site可以帮助你。它是一个Java注册表包装器,似乎有很多功能,但不知道实现有多强大。

答案 2 :(得分:0)

使用Otis回答以下代码很好。

static String getOutlookPath() {
  // Message message = new Message();
  final String classID;
  final String outlookPath;

  { // Fetch the Outlook Class ID
    int[] ret = RegUtil.RegOpenKey(RegUtil.HKEY_LOCAL_MACHINE,   "SOFTWARE\\Classes\\Outlook.Application\\CLSID", RegUtil.KEY_QUERY_VALUE);
    int handle = ret[RegUtil.NATIVE_HANDLE];
    byte[] outlookClassID = RegUtil.RegQueryValueEx(handle, "");

    classID = new String(outlookClassID).trim(); // zero terminated bytes
    RegUtil.RegCloseKey(handle);
  }

  { // Using the class ID from above pull up the path
    int[] ret = RegUtil.RegOpenKey(RegUtil.HKEY_LOCAL_MACHINE, "SOFTWARE\\Classes\\CLSID\\" + classID + "\\LocalServer32", RegUtil.KEY_QUERY_VALUE);
    int handle = ret[RegUtil.NATIVE_HANDLE];
    byte[] pathBytes = RegUtil.RegQueryValueEx(handle, "");

    outlookPath = new String(pathBytes).trim(); // zero terminated bytes
    RegUtil.RegCloseKey(handle);
  }

  return outlookPath;
}

答案 3 :(得分:0)

以下是一个与类似问题略有修改的解决方案:https://stackoverflow.com/a/6194710/854664 请注意,我使用的是.pst而不是.xls

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ShowOutlookInstalled {
    public static void main(String argv[]) {
        try {
            Process p = Runtime.getRuntime()
                    .exec(new String[] { "cmd.exe", "/c", "assoc", ".pst" });
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String extensionType = input.readLine();
            input.close();
            // extract type
            if (extensionType == null) {
                outlookNotFoundMessage("File type PST not associated with Outlook.");
            } else {
                String fileType[] = extensionType.split("=");

                p = Runtime.getRuntime().exec(
                        new String[] { "cmd.exe", "/c", "ftype", fileType[1] });
                input = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String fileAssociation = input.readLine();
                // extract path
                Pattern pattern = Pattern.compile("\".*?\"");
                Matcher m = pattern.matcher(fileAssociation);
                if (m.find()) {
                    String outlookPath = m.group(0);
                    System.out.println("Outlook path: " + outlookPath);
                } else {
                    outlookNotFoundMessage("Error parsing PST file association");
                }
            }

        } catch (Exception err) {
            err.printStackTrace();
            outlookNotFoundMessage(err.getMessage());
        }


    }

    private static void outlookNotFoundMessage(String errorMessage) {
        System.out.println("Could not find Outlook: \n" + errorMessage);

    }
}