I am using EWS to retrieve the folder path from a Microsoft Exchange inbox folder. However, I am getting a different result when the folder path is retrieved using FindFolderResult vs retrieving the path from the folder directly. Please note that, in both cases, the path returned is correct but in the second case (retrieved form the folder directly) the delimiter that separates the folder names is some unknown character.
I am using Visual Studio 2013 and the target framework is 4.5. I have demonstrated the issue in a console application but it is also present in a WPF application.
Questions
- Does anyone know why the folder strings are presented differently and how to fix the problem?
- If no fix is available I will probably just find and replace the unknown character but I am not sure what it is. The character displays as "?" in the console window but is absent in debugging hover over (ie no delimiter between names) and shows as a "[?]" (box with a question mark inside) when printed to immediate using debug.print. Any thoughts?
Please see code snippet and output sample below.
Code Snippet
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();
}
Output Sample
Output from Folder Results: \Inbox\Personal
Output from Bound Folder : ?Inbox?Personal
Output from Folder Results: \Inbox\Personal\Contacts
Output from Bound Folder : ?Inbox?Personal?Contacts
What fixed the issue for me
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();
}
Other posts that helped