有没有一种方法可以使用C#将PST文件导入Outlook? [英] Is there a way to import PST files into Outlook using C#?
本文介绍了有没有一种方法可以使用C#将PST文件导入Outlook?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用:Visual Studio 2017(语言:C#)
Using: Visual Studio 2017 (Language: C#)
我在下面的PowerShell脚本中有一个类似的函数,但是我需要它的C#版本才能在Visual Studio中单击按钮来执行:
I have a similar function written below in PowerShell script, but I need the C# version of it to execute on the click of a button within Visual Studio:
Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
dir "$env:userprofile\Documents\Outlook Files\*.pst" | % { $namespace.AddStore($_.FullName) }
任何见解或代码示例将不胜感激.
Any insight or examples of code would be much appreciated.
推荐答案
您可以通过以下方式进行操作:
You can do that the following way:
在您的项目中,右键单击引用",然后添加对程序集"Microsoft.Office.Interop.Outlook"的引用.
In your project, right click on "References" and add a reference to the assembly "Microsoft.Office.Interop.Outlook".
然后您可以使用以下代码:
Then you can use the following code:
/// <summary>
/// Get a reference to an already running or a newly started Outlook instance
/// </summary>
Microsoft.Office.Interop.Outlook.Application GetOutlookApp()
{
Microsoft.Office.Interop.Outlook.Application app = null;
// Try to get running instance
try
{
app = Marshal.GetActiveObject("Outlook.Application") as Microsoft.Office.Interop.Outlook.Application;
}
catch(Exception)
{
// Ignore exception when Outlook is not running
}
// When outlook was not running, try to start it
if(app == null)
{
app = new Microsoft.Office.Interop.Outlook.Application();
}
return app;
}
private void button1_Click(object sender, EventArgs e)
{
const string fileName = @"D:\MyDings.pst";
var app = GetOutlookApp();
var nameSpace = app.GetNamespace("MAPI");
nameSpace.AddStore(fileName);
MessageBox.Show("Done");
}
这篇关于有没有一种方法可以使用C#将PST文件导入Outlook?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文