gpt4 book ai didi

c# - 通过提供路径名称搜索公共(public)文件夹中的文件夹

转载 作者:太空狗 更新时间:2023-10-30 00:20:03 25 4
gpt4 key购买 nike

是否可以通过使用 Exchange Web 服务 (EWS) 托管 Api 提供文件夹路径来搜索公用文件夹中的所有文件夹和子文件夹?

最佳答案

您只能在文件夹内搜索,在 EWS 上深入一层,因此:

PublicFoldersRoot\subjectA\sectionB\partC\

我会搜索“subjectA”文件夹,然后在获得该 FolderId 后,我会搜索“sectionB”文件夹,依此类推,直到找到我需要的内容。

GetPublicFolderByPath 方法采用路径“subjectA\sectonB\partC\”并将路径拆分为文件夹名称数组,然后递归查找每个文件夹。

public Folder GetPublicFolderByPath(ExchangeService service, String ewsFolderPath)
{
String[] folders = ewsFolderPath.Split('\');

Folder parentFolderId = null;
Folder actualFolder = null;

for (int i = 0; i < folders.Count(); i++)
{
if (0 == i)
{
parentFolderId = GetTopLevelFolder(service, folders[i]);// for first first loop public folder root is the parent
actualFolder = parentFolderId; //in case folders[] is only one long
}
else
{
actualFolder = GetFolder(service, parentFolderId.Id, folders[i]);
parentFolderId = actualFolder;
}
}
return actualFolder;
}

GetTopLevelFolder 方法获取第一个文件夹“sectionA”,它是公共(public)文件夹根目录的子文件夹,也就是“WellKnownFolderName.PublicFoldersRoot”。

private Folder GetTopLevelFolder(ExchangeService service, String folderName)
{
FolderView folderView = new FolderView(int.MaxValue);
FindFoldersResults findFolderResults = service.FindFolders(WellKnownFolderName.PublicFoldersRoot, folderView);

foreach (Folder folder in findFolderResults)
{
if (folderName.Equals(folder.DisplayName, StringComparison.InvariantCultureIgnoreCase))
{
return folder;
}
}
throw new Exception("Top Level Folder not found: " + folderName);
}

GetFolder 方法采用父 FolderId 并在所有子文件夹中搜索与提供的名称匹配的项,并返回您请求的子 FolderId。

private Folder GetFolder(ExchangeService service, FolderId ParentFolderId, String folderName)
{
FolderView folderView = new FolderView(int.MaxValue);
FindFoldersResults findFolderResults = service.FindFolders(ParentFolderId, folderView);

foreach (Folder folder in findFolderResults)
{
if (folderName.Equals(folder.DisplayName, StringComparison.InvariantCultureIgnoreCase))
{
return folder;
}
}

throw new Exception("Folder not found: " + folderName);

}

请注意,我使用的是 Microsoft.Exchange.WebServices 托管 API dll,与 https://yourexchangeserver/ews/services.wsdl 的想法类似。要从路径中获取文件夹,请使用创建 ExchangeService 对象然后写入:
GetPublicFolderByPath(服务, "subjectA\sectionB\partC\")

如果对您有帮助请点赞:)

关于c# - 通过提供路径名称搜索公共(public)文件夹中的文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14561124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com