@".NET Developer, ERP and
     Database Analyst"

Outlook Mail Signatures

I recently ran into the problem of getting the local user’s mail signature for an automated email program, after much head scratching – I came up with this function that returns the signature – as long as it is Windows Vista and up, and Outlook 2007 and up.

VB version (C# below)

Private Function ReadSignature() As String
   Dim appDataDir As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Microsoft\Signatures"
   Dim signature As String = String.Empty
   Dim diInfo As New DirectoryInfo(appDataDir)
   If diInfo.Exists Then
      Dim fiSignature As FileInfo() = diInfo.GetFiles("*.htm")
      If fiSignature.Length > 0 Then
         Dim sr As New StreamReader(fiSignature(0).FullName, Encoding.[Default])
         signature = sr.ReadToEnd()

         If Not String.IsNullOrEmpty(signature) Then
            Dim fileName As String = fiSignature(0).Name.Replace(fiSignature(0).Extension, String.Empty)
            signature = signature.Replace(fileName & "_files/", appDataDir & "/" & fileName & "_files/")
         End If
      End If
   End If

   Return signature
End Function

C# version

private string ReadSignature()
        {
            string appDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Microsoft\\Signatures";
            string signature = string.Empty;
            DirectoryInfo diInfo = new DirectoryInfo(appDataDir);
            if (diInfo.Exists)
            {
                FileInfo[] fiSignature = diInfo.GetFiles("*.htm");
                if (fiSignature.Length > 0)
                {
                    StreamReader sr = new StreamReader(fiSignature[0].FullName, Encoding.Default);
                    signature = sr.ReadToEnd();

                    if (!string.IsNullOrEmpty(signature))
                    {
                        string fileName = fiSignature[0].Name.Replace(fiSignature[0].Extension, string.Empty);
                        signature = signature.Replace(fileName + "_files/", appDataDir + "/" + fileName + "_files/");
                    }
                }
            }

            return signature;
        }