private static string GetName(IMoniker moniker)
{
// Declare variables
Object bagObj = null;
IPropertyBag bag = null;
try
{
// Bind the moniker to storage
Guid bagId = typeof (IPropertyBag).GUID;
moniker.BindToStorage(null, null, ref bagId, out bagObj);
bag = (IPropertyBag) bagObj;
// Try to retrieve the friendly name
object val = "";
int hr = bag.Read("FriendlyName", ref val, IntPtr.Zero);
if (hr != 0)
{
Marshal.ThrowExceptionForHR(hr);
}
// Convert to string & validate
string result = (string) val;
if (string.IsNullOrEmpty(result))
{
throw new ApplicationException();
}
// Return result
return result;
}
catch (Exception)
{
// Return empty string
return string.Empty;
}
finally
{
// Clean up
bag = null;
if (bagObj != null)
{
Marshal.ReleaseComObject(bagObj);
bagObj = null;
}
}
}