我正在使用EWS从Microsoft Exchange收件箱文件夹中检索文件夹路径。但是,当使用FindFolderResult检索文件夹路径与直接从文件夹中检索路径时,我得到的结果不同。请注意,在这两种情况下,返回的路径都是正确的,但在第二种情况下(直接从文件夹中检索),分隔文件夹名称的分隔符是一些未知字符。
我使用的是Visual Studio 2013,目标框架是4.5。我已在控制台应用程序中演示了该问题,但它也存在于WPF应用程序中。
问题
请参阅下面的代码段和输出示例。
代码段
static void Main(string[] args)
{
string EmailAccount = "someemail@somedomain.com";
ExchangeService myService = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
ServicePointManager.ServerCertificateValidationCallback =
CertificateValidationCallBack;
myService.UseDefaultCredentials = true;
myService.AutodiscoverUrl(EmailAccount, RedirectionUrlValidationCallback);
ExtendedPropertyDefinition PR_Folder_Path = new
ExtendedPropertyDefinition(26293, MapiPropertyType.String);
PropertySet myPropertySet = new PropertySet(BasePropertySet.IdOnly,
FolderSchema.DisplayName, PR_Folder_Path);
FolderView myFolderView = new FolderView(100);
myFolderView.PropertySet = myPropertySet;
myFolderView.Traversal = FolderTraversal.Deep;
FolderId targetFolder = WellKnownFolderName.Inbox;
FindFoldersResults myFolderResults =
myService.FindFolders(targetFolder, myFolderView);
foreach (var f in myFolderResults)
{
//Shows path text when retreived from FindFolderResults.
string s;
f.TryGetProperty(PR_Folder_Path, out s);
Console.WriteLine("Output from Folder Results: {0}", s);
//Binds the folder and show path text when retrieve from
//directly from the bound folder.
string t;
Folder myTestFolder = Folder.Bind(myService, f.Id, myPropertySet);
myTestFolder.TryGetProperty(PR_Folder_Path, out t);
Console.WriteLine("Output from Bound Folder : {0}", t);
}
Console.ReadLine();
}
输出样本
文件夹结果输出:\ Inbox \ Personal
绑定文件夹的输出:?收件箱?个人
文件夹结果输出:\ Inbox \ Personal \ Contacts
绑定文件夹的输出:?收件箱?个人?联系人
为我解决了什么问题
string t;
string c;
Folder myTestFolder = Folder.Bind(myService, f.Id, myPropertySet);
myTestFolder.TryGetProperty(PR_Folder_Path, out t);
byte[] tBytes = Encoding.Default.GetBytes(t);
var hexString = BitConverter.ToString(tBytes);
hexString = hexString.Replace("3F", "5C");
c = System.Text.Encoding.Default.GetString(StringToByteArray(hexString));
public static byte[] StringToByteArray(string hex)
{
hex = hex.Replace("-", "");
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
其他有帮助的帖子
答案 0 :(得分:0)
它返回的字节顺序标记有多种方法可以在字符串中替换它们使用
Encoding.Unicode.GetString(HexConvertor.HexStringToByteArray(BitConverter.ToString(UnicodeEncoding.Unicode.GetBytes((String)FolderPathValue)).Replace("FE-FF", "5C-00")
但还有其他方法可以做得更清洁,更快
在FindFolder中使用该属性时要小心的是,如果值的大小超过256个字节,它将被截断,您需要使用Bind / GetFolder来获取整个值。