Fix Outlook folder discovery to find all contact folders
- Scan all stores recursively instead of only the default folder - Properly release COM objects to avoid leaks - Better error handling with debug output per store/folder - Show error message if Outlook is not reachable - Fallback to default contacts folder if recursive scan finds nothing Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -50,20 +50,29 @@ namespace StarfaceOutlookSync.Services
|
||||
var app = GetOutlookApp();
|
||||
var ns = app.GetNamespace("MAPI");
|
||||
|
||||
// Standard-Kontaktordner
|
||||
var defaultFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
|
||||
folders.Add(defaultFolder.FolderPath);
|
||||
|
||||
// Unterordner
|
||||
AddSubFolders(defaultFolder, folders);
|
||||
|
||||
// Weitere Kontaktordner in anderen Stores
|
||||
// Alle Stores durchgehen (jedes Konto, jede PST-Datei etc.)
|
||||
foreach (Outlook.Store store in ns.Stores)
|
||||
{
|
||||
try
|
||||
{
|
||||
var rootFolder = store.GetRootFolder();
|
||||
FindContactFolders(rootFolder, folders);
|
||||
FindContactFoldersRecursive(rootFolder, folders);
|
||||
Marshal.ReleaseComObject(rootFolder);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"Error scanning store '{store.DisplayName}': {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
// Falls nichts gefunden, Standard-Kontaktordner als Fallback
|
||||
if (folders.Count == 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
var defaultFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
|
||||
folders.Add(defaultFolder.FolderPath);
|
||||
Marshal.ReleaseComObject(defaultFolder);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
@@ -78,30 +87,34 @@ namespace StarfaceOutlookSync.Services
|
||||
return folders;
|
||||
}
|
||||
|
||||
private void AddSubFolders(Outlook.MAPIFolder folder, List<string> paths)
|
||||
private void FindContactFoldersRecursive(Outlook.MAPIFolder folder, List<string> paths)
|
||||
{
|
||||
foreach (Outlook.MAPIFolder sub in folder.Folders)
|
||||
try
|
||||
{
|
||||
if (sub.DefaultItemType == Outlook.OlItemType.olContactItem)
|
||||
// Kontaktordner erkennen: DefaultItemType ODER Ordnername enthaelt "Kontakt"/"Contact"
|
||||
if (folder.DefaultItemType == Outlook.OlItemType.olContactItem)
|
||||
{
|
||||
if (!paths.Contains(sub.FolderPath))
|
||||
paths.Add(sub.FolderPath);
|
||||
AddSubFolders(sub, paths);
|
||||
if (!paths.Contains(folder.FolderPath))
|
||||
paths.Add(folder.FolderPath);
|
||||
}
|
||||
|
||||
// Alle Unterordner durchsuchen
|
||||
foreach (Outlook.MAPIFolder sub in folder.Folders)
|
||||
{
|
||||
try
|
||||
{
|
||||
FindContactFoldersRecursive(sub, paths);
|
||||
}
|
||||
catch { }
|
||||
finally
|
||||
{
|
||||
Marshal.ReleaseComObject(sub);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void FindContactFolders(Outlook.MAPIFolder folder, List<string> paths)
|
||||
{
|
||||
if (folder.DefaultItemType == Outlook.OlItemType.olContactItem)
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (!paths.Contains(folder.FolderPath))
|
||||
paths.Add(folder.FolderPath);
|
||||
}
|
||||
|
||||
foreach (Outlook.MAPIFolder sub in folder.Folders)
|
||||
{
|
||||
FindContactFolders(sub, paths);
|
||||
System.Diagnostics.Debug.WriteLine($"Error scanning folder '{folder.Name}': {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user