Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Outlook:“关闭邮件项目”对话框也将关闭Outlook应用程序_C#_Outlook - Fatal编程技术网

C# Outlook:“关闭邮件项目”对话框也将关闭Outlook应用程序

C# Outlook:“关闭邮件项目”对话框也将关闭Outlook应用程序,c#,outlook,C#,Outlook,我对以下代码有问题: 条件1是,Outlook在主服务器启动时未运行!!! 状态2 Outlook未从任务栏中显示 class Program { private static Application _appl; private static void Main(string[] args) { Sample sample = new Sample(); Console.WriteLine(sample.IsOutlookRunning

我对以下代码有问题:

条件1是,Outlook在主服务器启动时未运行!!! 状态2 Outlook未从任务栏中显示

class Program
{
    private static Application _appl;

    private static void Main(string[] args)
    {
        Sample sample = new Sample();
        Console.WriteLine(sample.IsOutlookRunning());

        _appl = sample.GetApplicationObject();

        Thread.Sleep(new TimeSpan(0, 0, 0, 5));
        if (sample.IsOutlookRunning())
        {
            Console.WriteLine(sample.IsOutlookRunning());
        }

        Console.ReadKey();
        MailItem mailItem = _appl.CreateItem(OlItemType.olMailItem);
        mailItem.Display();

        Thread.Sleep(new TimeSpan(0, 0, 0, 5));

        //_appl ist not running more!!!!!!!!!!!!!!!!!!!!!

        Console.ReadKey();
        MailItem mailItem2 = _appl.CreateItem(OlItemType.olMailItem);
        mailItem2.Display();

        Console.ReadKey();
    }
}


Outlook在其最后一个窗口关闭时关闭,即使存在对其任何对象的未完成引用。这是在Outlook2007的时间框架内故意做的,目的是防止外部应用程序在用户想要关闭Outlook时保持其打开状态


如果我没记错的话,您可以通过保留对检查器或资源管理器对象(例如MailItem.GetInspector或MAPIFolder.GetExplorer())的引用来阻止Outlook关闭。

是否尝试调试代码?你有例外吗?
public class Sample
{
    private Outlook.Application _application;

    public Outlook.Application GetApplicationObject()
    {

        // Check whether there is an Outlook process running.
        if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
        {

            // If so, use the GetActiveObject method to obtain the process and cast it to an Application object.
            _application = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
        }
        else
        {
            // If not, create a new instance of Outlook and log on to the default profile.
            _application = new Outlook.Application();
            Outlook.NameSpace nameSpace = _application.GetNamespace("MAPI");
            nameSpace.Logon(Missing.Value, Missing.Value, false, Missing.Value);
            nameSpace = null;
        }

        // Return the Outlook Application object.
        return _application;
    }
}