using System; using System.Collections.Generic; using System.IO; using Microsoft.Win32; using Outlook = Microsoft.Office.Interop.Outlook; namespace BulkArchiveMailer.AddIn { /// /// Reads the user's default "new message" Outlook signature HTML from /// disk. Outlook Interop has no API that exposes "the currently /// selected default signature" - the only reliable way to get at it /// is: look up its *name* in the registry /// (HKCU\Software\Microsoft\Office\<version>\Common\MailSettings, /// value "NewSignature"), then load the matching .htm file from /// %APPDATA%\Microsoft\Signatures\<name>.htm ourselves. /// /// This is the only file in the solution that touches the Windows /// registry - deliberately kept out of BulkArchiveMailer.Core. /// public static class SignatureReader { /// /// Best-effort auto-detection. Never throws - returns null if the /// registry value, the version-specific key, or the .htm file is /// missing for any reason, so the caller can fall back to letting /// the operator pick a signature file manually instead of crashing /// the add-in. /// public static string TryAutoDetectSignatureHtml(Outlook.Application application) { try { var signatureName = TryReadSignatureName(application); if (string.IsNullOrEmpty(signatureName)) { return null; } var path = ResolveSignaturePath(signatureName); return File.Exists(path) ? File.ReadAllText(path) : null; } catch { return null; } } /// Same lookup, but returns the resolved .htm path instead of its contents - useful for SettingsForm to show/prefill the path. public static string TryAutoDetectSignaturePath(Outlook.Application application) { try { var signatureName = TryReadSignatureName(application); if (string.IsNullOrEmpty(signatureName)) { return null; } var path = ResolveSignaturePath(signatureName); return File.Exists(path) ? path : null; } catch { return null; } } public static string ResolveSignaturePath(string signatureName) { var signaturesFolder = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Microsoft", "Signatures"); return Path.Combine(signaturesFolder, signatureName + ".htm"); } private static string TryReadSignatureName(Outlook.Application application) { var candidateVersions = new List(); try { // application.Version looks like "16.0" and lines up // exactly with the Office registry hive layout, so prefer // it over the fixed fallback list below. var version = application?.Version; if (!string.IsNullOrEmpty(version)) { candidateVersions.Add(version); } } catch { // Interop call failed for some reason - fall through to // the fixed list. } foreach (var fallback in new[] { "16.0", "15.0", "14.0" }) { if (!candidateVersions.Contains(fallback)) { candidateVersions.Add(fallback); } } foreach (var version in candidateVersions) { using (var key = Registry.CurrentUser.OpenSubKey( $@"Software\Microsoft\Office\{version}\Common\MailSettings")) { var value = key?.GetValue("NewSignature") as string; if (!string.IsNullOrEmpty(value)) { return value; } } } return null; } } }