Partager via


Code C# utilisant WebDAV pour calculer la taille d'une boîte aux lettres Exchange 2000/2003

Voici un code en C# qui vous permettra d'obtenir la taille d'une boîte aux lettres Exchange. Le résultat retourné est identique à celui que vous retrouvez dans l'ESM (Exchange System Manager).

Ce code reprend simplement le code proposé dans cet article technique : https://support.microsoft.com/default.aspx?scid=kb;en-us;320071

Sous Visual Studio, créez une solution de type "Console Application" appélée RecursiveLoopFolders, ajoutez une classe que vous nommerez WebDAVSize.cs. Copier/coller dans cette classe le code suivant:

using System;

using System.Net;

using System.IO;

using System.Text;

using System.Xml;

namespace RecursiveLoopPublicFolders

{

      public class WebDAVSize

      {

            static public long FolderSize(string URL, string userName, string userPassword, string userDomain)

            {

                  System.Net.HttpWebRequest Request;

                  System.Net.WebResponse Response;

                  System.Net.CredentialCache MyCredentialCache;

                  string strQuery ="";

                  byte[] bytes = null;

                  System.IO.Stream RequestStream = null;

                  System.IO.Stream ResponseStream = null;

                  XmlDocument ResponseXmlDoc = null;

                 

                  XmlNodeList oXMLSizeNodes = null;

                  XmlNodeList oXMLHREFNodes = null;

                  XmlNodeList oXMLHasSubsNodes = null;

                  long totalSize=0;

                  try

                  {

                        strQuery = @"<?xml version=""1.0""?>";

                        strQuery += @"<g:searchrequest xmlns:g=""DAV:"">";

                        strQuery += @"<g:sql>SELECT ""https://schemas.microsoft.com/";

                        strQuery += @"mapi/proptag/x0e080003"", ""DAV:hassubs"" FROM SCOPE ";

                        strQuery += "('SHALLOW TRAVERSAL OF \"" + URL + "\"') ";

                        strQuery += @"WHERE ""DAV:isfolder"" = true";

                        strQuery += @"</g:sql>";

                        strQuery += @"</g:searchrequest>";

                        MyCredentialCache = new System.Net.CredentialCache();

                        MyCredentialCache.Add( new System.Uri(URL),

                             "NTLM",

                             new System.Net.NetworkCredential(userName, userPassword, userDomain)

                             );

                        Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(URL);

                        Request.Credentials = MyCredentialCache;

                        Request.Method = "SEARCH";

                        bytes = Encoding.UTF8.GetBytes((string)strQuery);

                        Request.ContentLength = bytes.Length;

                        RequestStream = Request.GetRequestStream();

                        RequestStream.Write(bytes, 0, bytes.Length);

                        RequestStream.Close();

                        Request.ContentType = "text/xml";

                        Request.Headers.Add("Translate", "f");

                        Request.Headers.Add("Depth", "0");

                        Response = (HttpWebResponse)Request.GetResponse();

                        ResponseStream = Response.GetResponseStream();

                        ResponseXmlDoc = new XmlDocument();

                        ResponseXmlDoc.Load(ResponseStream);

                        // Get the XML nodes that contain the individual sizes.

                        oXMLSizeNodes = ResponseXmlDoc.GetElementsByTagName("d:x0e080003");

            oXMLHREFNodes = ResponseXmlDoc.GetElementsByTagName("a:href");

                        oXMLHasSubsNodes = ResponseXmlDoc.GetElementsByTagName("a:hassubs");

     

                        // Loop through the nodes, and then add all of the sizes.

                        for (int i=0; i < oXMLSizeNodes.Count; i++)

                        {

                             totalSize += long.Parse(oXMLSizeNodes.Item(i).InnerText);

                            

                             if (oXMLHasSubsNodes.Item(i).InnerText == "1")

                             {

                                   totalSize += FolderSize(oXMLHREFNodes.Item(i).InnerText, userName, userPassword, userDomain);

                             }

                        }

                        ResponseStream.Close();

                        Response.Close();

                       

                        return totalSize;

                  }

                  catch(Exception ex)

                  {

                        Console.WriteLine(ex.Message);

                        return 0;

                  }

            }

      }

}

Ensuite, voici le code de l'appelant:

[STAThread]

static void Main(string[] args)

{

      long totalSizeO;

      double totalSizeKO;

      string userName = "exchangesuperuser";

      totalSizeO = WebDAVSize.FolderSize("https://davrous2005/exchange/" + userName + "/NON_IPM_SUBTREE", "exchangesuperuser", "password!1", "DAVROUS2K3");

      Console.WriteLine ("******************************************");

      Console.WriteLine ("* TOTAL FINAL SIZE : " + totalSizeO + " Octets");

      totalSizeKO = totalSizeO/(double)1024;

      totalSizeKO = Math.Ceiling(totalSizeKO);

      Console.WriteLine ("* TOTAL FINAL SIZE : " + totalSizeKO + " KO");

      Console.WriteLine ("******************************************");

      Console.ReadLine();

}

Pour terminer, voici le résultalt sur le serveur Exchange 2003 vu plus haut:

Le résultat est bien identique.

-= David =-