Okay, I gave up I am trying to pull the LastLogon time from Active Directory and I'm having all sorts of issues with the conversion. I know a simple internet search pulls up dozens of examples and I've tried every single one. If anyone could please tell me where I'm going wrong it would be greatly appreciated...

Try 1:
I see, you are using a SearchResultsCollection, here the property value is already converted to a long (from a LARGEINTERGER).

Try this:
...
long lastLogon = (long)results.Properties[Key][0]
string dt = DateTime.FromFileTime(lastLogon ).ToString();

I tried both that and this...

DateTime ll = DateTime.FromFileTime((long)Results.Properties["LastLogon"][0]);

- This gives a date and time at least, unfortunately its not the correct date and time when compared against adsi. I am using the SearchResultsCollection. Is there a better way?

Try 2:
LargeInteger li = Results.Properties["LastLogon"][0] as LargeInteger;
long expDate = (((long)(li.HighPart) << 32) + (long)li.LowPart);
string dt = DateTime.FromFileTime(expDate).ToString();

- This give a "object reference not set to instance of an object" exception. Taken from many of the search results I also made 2 modifications. The original example had a Results.Properties["LastLogon"].value (.value on the end without the [0]) which is not valid. Also the last line stated just Date instead of expDate. This seems to be the most popular example but I'm not sure why I cant get it to work.

Try 3:
ActiveDs.IADsLargeInteger li = ((ActiveDs.IADsLargeInteger)(Results.Properties["LastLogon"][0]));
long date = (long)li.HighPart << 32 | (uint)li.LowPart;
DateTime dt = DateTime.FromFileTime(date);

- This also originally had Results.Properties["LastLogon"].value. Why does it say for me that this is not valid? Using the above gives an exception "unable to case object of type system.int64 to type activeds.iadslargeinteger" There are also a few others that are similar to this all result in this exception.

Try 4:
LargeInteger largeInt = (LargeInteger)Results.Properties["lastLogon"][0];
Int64 liTicks = largeInt.HighPart * 0x100000000 + largeInt.LowPart;
if (DateTime.MaxValue.Ticks >= liTicks && DateTime.MinValue.Ticks <= liTicks) {DateTime dTemp = DateTime.FromFileTime(liTicks);}

- Same as Try 3.


I keep trying...but they mostly seem to be variants of one of the above. If anyone can assist that would be great. Maybe not using the SearchResultsCollection is better I'll keep researching...

Thanks