使用:Visual Studio 2017(语言:C#)
我在下面用 PowerShell 脚本编写了一个类似的函数,但我需要它的 C# 版本才能在 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) }
任何见解或代码示例将不胜感激。
最佳答案
您可以通过以下方式做到这一点:
在您的项目中,右键单击“引用”并添加对程序集“Microsoft.Office.Interop.Outlook”的引用。
然后你可以使用下面的代码:
/// <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# - 有没有办法使用 C# 将 PST 文件导入 Outlook?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49559535/