使用 c# 启动 Outlook
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/824311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Start Outlook with c#
提问by jeje1983
Can I make C# start Outlook in the code?
我可以让 C# 在代码中启动 Outlook 吗?
In VB6 we use object 'Outlook.Application' and write:'
在 VB6 中,我们使用对象“Outlook.Application”并写入:
Set oOutlook = CreateObject("Outlook.Application")
Set oNameSpace = oOutlook.GetNamespace("MAPI")
Set oInbox = oNameSpace.Folders(1)
'Set oInbox = oInbox.Folders("Inbox")
oInbox.Display
'oOutlook.Quit 'Close All Outlook copies
Copy/Paste from link: http://www.ozgrid.com/forum/showthread.php?t=73886
从链接复制/粘贴:http://www.ozgrid.com/forum/showthread.php?t =73886
采纳答案by Chathuranga Chandrasekara
If you just want to start the outlook; using System.Diagnostics.Processwould be the easiest way. :)
如果你只是想开始展望;使用System.Diagnostics.Process将是最简单的方法。:)
回答by abatishchev
System.Diagnostics.Process
will only start a process.
System.Diagnostics.Process
只会启动一个进程。
To do additional actions such choosing folders, you need to use Microsoft Visual Studio Tools for Office (VSTO). And hereis it's reference. For example:
要执行其他操作,例如选择文件夹,您需要使用 Microsoft Visual Studio Tools for Office (VSTO)。而这里是它的参考。例如:
var outlook = new Microsoft.Office.Interop.Outlook.ApplicationClass();
outlook.Quit();
回答by Ricardo Amores
You could use its ProgID to get the type and the activator
您可以使用它的 ProgID 来获取类型和激活器
Type objectType = Type.GetTypeFromProgID("Outlook.Application");
object outlook = Activator.CreateInstance(objectType);
But using this in C# you will lose all type information (i.e. no IntelliSense) and you need to call some ugly method to invoke the operations with LateBinding (google for Type.Invoke)
但是在 C# 中使用它你将丢失所有类型信息(即没有智能感知),你需要调用一些丑陋的方法来调用具有 LateBinding 的操作(谷歌搜索 Type.Invoke)
Other option is add a reference to Microsoft.Office.Interop.Outlook.ApplicationClass, so you have compile time type information and create an instance for Outlook in the usual way
其他选项是添加对 Microsoft.Office.Interop.Outlook.ApplicationClass 的引用,以便您获得编译时类型信息并以通常的方式为 Outlook 创建一个实例
using Microsoft.Office.Interop.Outlook;
Microsoft.Office.Interop.Outlook.ApplicationClass outlook
= new Microsoft.Office.Interop.Outlook.ApplicationClass();
Or you could use my Late Binding Helperlibrary and use it like this
或者你可以使用我的Late Binding Helper库并像这样使用它
Invoker outlook = BindingFactory.CreateAutomationBinding("Outlook.Application");
outlook.Method("Quit").Invoke();
No Intellisense with this one, but at least the library will save you from the ugly calls to Type.Invoke and give you a fluent interface instead.
这个没有 Intellisense,但至少该库将使您免于对 Type.Invoke 的丑陋调用,并为您提供流畅的界面。
回答by B. Clay Shannon
This works (you might have to change the path to what it is on the machine on which the app will run):
这有效(您可能需要将路径更改为运行应用程序的机器上的路径):
public static void StartOutlookIfNotRunning()
{
string OutlookFilepath = @"C:\Program Files (x86)\Microsoft Office\Office12\OUTLOOK.EXE";
if (Process.GetProcessesByName("OUTLOOK").Count() > 0) return;
Process process = new Process();
process.StartInfo = new ProcessStartInfo(OutlookFilepath);
process.Start();
}