Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Outlook Objects... Managed or Unmanaged?
Message
De
05/07/2007 15:39:09
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Titre:
Outlook Objects... Managed or Unmanaged?
Divers
Thread ID:
01238064
Message ID:
01238064
Vues:
92
Are outlook objects managed or unmanaged under the CLR?

Roughing out an outllok class trying figure out if I have the IDispose set up right.

(Still very very rough, see protected void Dispose(bool Disposing) at end of code)

if you see something else wrong... any and all comments are welcome.
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using Microsoft.Win32;
using System.Globalization;

namespace cdsOutlook
  {
  public class cdsOutlook : IDisposable 
    {

    private bool IsDisposed=false;
    public static Outlook.Application oApp;
    public static Outlook.NameSpace oNS;
    public static Outlook.MAPIFolder oInbox;
    public static Outlook.Items oItems;
    public static Outlook.MailItem oMsg;

    /// <summary>
    /// Fix this routine to be the constructor */*/*/*/*/*/*
    /// </summary>
    /// <param name="args"></param>
    /// <returns></returns>
    public static int Temp()
      {
      try
        {
        // Create the Outlook application.
        // in-line initialization
        oApp = new Outlook.Application();

        // Get the MAPI namespace.
        oNS = oApp.GetNamespace("mapi");

        // Log on by using the default profile or existing session (no dialog box).
        oNS.Logon(Missing.Value, Missing.Value, false, true);

        // Alternate logon method that uses a specific profile name.
        // TODO: If you use this logon method, specify the correct profile name
        // and comment the previous Logon line.
        //oNS.Logon("profilename",Missing.Value,false,true);

        //Get the Inbox folder.
        oInbox = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

        //Get the Items collection in the Inbox folder.
        oItems = oInbox.Items;

        // Get the first message.
        // Because the Items folder may contain different item types,
        // use explicit typecasting with the assignment.
        oMsg = (Outlook.MailItem)oItems.GetFirst();
        }
      //Error handler.
      catch (System.Exception e)
        {
        //Console.WriteLine("{0} Exception caught: ", e);
        }
      //Console.ReadLine();
      // Return value.	
      return 0;

      }
 
    /// <summary>
    /// Is Outlook Installed
    /// </summary>
    /// <returns>bool</returns>
    private bool IsOutlookInstalled()
      {
      Type requestType = Type.GetTypeFromProgID("Outlook.Application", false);
      if (requestType == null)
        {
        RegistryKey key = null;
        try
          {
          key = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Office", false);
          if (key != null)
            {
            double version = 0.0, temp = 0.0;
            string[] valueNames = key.GetSubKeyNames();
            for (int i = 0; i < valueNames.Length; i++)
              {
              temp = 0.0;
              try
                {
                temp = Convert.ToDouble(valueNames[i],
                CultureInfo.CreateSpecificCulture("en-US").NumberFormat);
                }
              catch
                {
                }
              if (temp > version) version = temp;
              }
            key.Close();
            if (version != 0.0)
              requestType = Type.GetTypeFromProgID("Outlook.Application." + version.ToString().Replace(",", "."), false);
            }
          }
        catch
          {
          if (key != null) key.Close();
          }
        }
      return (requestType != null);
      }

    /// <summary>
    /// Get Office Version
    /// </summary>
    /// <returns>int Version Number</returns>
    private int GetOfficeVersion()
      {
      RegistryKey key = null;
      try
        {
        key = Registry.ClassesRoot.OpenSubKey("Outlook.Application\\CurVer", false);
        if (key != null)
          {
          string version = key.GetValue("", "Outlook.Application.9").ToString(); key.Close();
          int pos = version.LastIndexOf(".");
          if (pos >= 0)
            {
            version = version.Remove(0, pos + 1);
            return Convert.ToInt32(version);
            }
          }
        }
      catch (Exception e)
        {
        // */* need error handler
        if (key != null) key.Close();
          //module.DoError(e);
        }
      return 9;
      }

    /// <summary>
    /// Explicit Dispose - Close Outlook, call prior to release.
    /// </summary>
    public void Dispose()
      {
      Dispose(true);
      GC.SuppressFinalize(this);
      }

    /// Internal Destructor Code
    protected void Dispose(bool Disposing)
      {
      if(!IsDisposed)
        {
        if(Disposing)
          {
          // */* Clean up managed resources
          }
        // */* Clean up unmanaged resources
        //Log off.
        oNS.Logoff();
        //Explicitly release objects.
        oMsg = null;
        oItems = null;
        oInbox = null;
        oNS = null;
        oApp = null;
        }
       IsDisposed=true;
       }

    /// Destructor
    ~cdsOutlook()
      {
      Dispose(false);
      }
    }
  }
Répondre
Fil
Voir

Click here to load this message in the networking platform