public static bool SetProperty(DirectoryEntry entry, string propertyName, object value)
        {
            bool   contains     = entry.Properties.Contains(propertyName);
            object currentValue = contains ? entry.Properties[propertyName].Value : null;

            if (OSTrace.LogLevel.TraceVerbose)
            {
                OSTrace.Debug("OSDirectoryEntry.SetProperty([DirectoryEntry.Path='{0}'], {1}{2}", entry.Path, propertyName, contains ? ("[Current='" + Convert.ToString(currentValue) + "'] = ") : " = ", Convert.ToString(value) + ")");
            }

            if (value == null && (!contains || currentValue == null))
            {
                return(false);
            }
            else if (value == null || // unset the property
                     value is ICollection) // Not going to try even check array values since all other insert methods fail with COMExceptions most of the times
            {
                entry.Properties[propertyName].Value = value;
                return(true);
            }
            else if (contains)     // If it is null, cant set a new non-array value or we get COMException 0x80005005
            {
                if (!Object.Equals(currentValue, value))
                {
                    entry.Properties[propertyName][0] = value;
                    return(true);
                }
            }
            else     // if (!contains)
            {
                entry.Properties[propertyName].Add(value);
                return(true);
            }
            return(false);
        }
        public static object GetProperty(DirectoryEntry entry, string propertyName)
        {
            object result = entry.Properties[propertyName].Value;

            if (OSTrace.LogLevel.TraceVerbose)
            {
                OSTrace.Debug("OSDirectoryEntry.GetProperty([DirectoryEntry.Path='{0}'], {1} = {2})", entry.Path, propertyName, result ?? "null");
            }
            return(result);
        }
 public static void CommitChanges(DirectoryEntry entry)
 {
     OSTrace.Info("OSDirectoryEntry.CommitChanges([DirectoryEntry.Path='" + entry.Path + "'])\n" + GetCallingStack());
     try {
         entry.CommitChanges();
     } catch (Exception e) {
         OSTrace.Error("OSDirectoryEntry.CommitChanges([DirectoryEntry.Path='" + entry.Path + "'])", e);
         throw e;
     }
     OSTrace.Info("Finish OSDirectoryEntry.CommitChanges([DirectoryEntry.Path='" + entry.Path + "'])");
 }
        public static DirectoryEntries GetChildren(DirectoryEntry entry)
        {
            OSTrace.Debug("OSDirectoryEntry.GetChildren([DirectoryEntry.Path='" + entry.Path + "'])\n" + GetCallingStack());
            DirectoryEntries entries = null;

            try {
                entries = entry.Children;
            } catch (Exception e) {
                OSTrace.Error("OSDirectoryEntry.GetChildren([DirectoryEntry.Path='" + entry.Path + "'])\n", e);
                throw e;
            }
            OSTrace.Debug("Finish OSDirectoryEntry.GetChildren([DirectoryEntry.Path='" + entry.Path + "'])");
            return(entries);
        }
        public static bool Exists(string path)
        {
            OSTrace.Debug("OSDirectoryEntry.Exists('" + path + "')\n" + GetCallingStack());
            bool result = false;

            try {
                result = DirectoryEntry.Exists(path);
            } catch (Exception e) {
                OSTrace.Error("OSDirectoryEntry.Exists('" + path + "')", e);
                throw e;
            }
            OSTrace.Debug("Finish OSDirectoryEntry.Exists('" + path + "')");
            return(result);
        }
        public static DirectoryEntry New(string path)
        {
            OSTrace.Debug("OSDirectoryEntry.New('" + path + "')\n" + GetCallingStack());
            DirectoryEntry entry = null;

            try {
                entry = new DirectoryEntry(path);
            } catch (Exception e) {
                OSTrace.Error("OSDirectoryEntry.New('" + path + "')", e);
                throw e;
            }
            OSTrace.Debug("Finish OSDirectoryEntry.New('" + path + "')");
            return(entry);
        }
        public static void DeleteTree(DirectoryEntry entry)
        {
            if (OSTrace.LogLevel.TraceVerbose)
            {
                OSTrace.Info("Start OSDirectoryEntry.DeleteTree([DirectoryEntry.Path='" + entry.Path + "'])\n" + GetCallingStack());
            }

            try {
                entry.DeleteTree();
            } catch (Exception e) {
                OSTrace.Error("OSDirectoryEntry.DeleteTree([DirectoryEntry.Path='" + entry.Path + "'])", e);
                throw e;
            }
            OSTrace.Info("Finish OSDirectoryEntry.DeleteTree([DirectoryEntry.Path='" + entry.Path + "'])");
        }
        public static void RemoveChildren(DirectoryEntry entry, DirectoryEntry entryToRemove)
        {
            if (OSTrace.LogLevel.TraceVerbose)
            {
                OSTrace.Debug("Start OSDirectoryEntry.RemoveChildren([DirectoryEntry.Path='" + entry.Path + "'], [DirectoryEntryToRemove.Path='" + entryToRemove.Path + "'])\n" + GetCallingStack());
            }

            try {
                entry.Children.Remove(entryToRemove);
            } catch (Exception e) {
                OSTrace.Error("OSDirectoryEntry.RemoveChildren([DirectoryEntry.Path='" + entry.Path + "'], [DirectoryEntryToRemove.Path='" + entryToRemove.Path + "'])", e);
                throw e;
            }
            OSTrace.Debug("Finish OSDirectoryEntry.RemoveChildren([DirectoryEntry.Path='" + entry.Path + "'], [DirectoryEntryToRemove.Path='" + entryToRemove.Path + "'])");
        }
        /// <remarks>Must use CommitChanges before any property reads.</remarks>
        public static DirectoryEntry AddChildren(DirectoryEntry entry, string name, string schemaClassName)
        {
            if (OSTrace.LogLevel.TraceVerbose)
            {
                OSTrace.Debug("OSDirectoryEntry.AddChildren([DirectoryEntry.Path='" + entry.Path + "'], '" + name + "', '" + schemaClassName + "')\n" + GetCallingStack());
            }
            DirectoryEntry result = null;

            try {
                result = entry.Children.Add(name, schemaClassName);
            } catch (Exception e) {
                OSTrace.Error("OSDirectoryEntry.AddChildren([DirectoryEntry.Path='" + entry.Path + "'], '" + name + "', '" + schemaClassName + "')", e);
                throw e;
            }
            OSTrace.Debug("Finish OSDirectoryEntry.AddChildren([DirectoryEntry.Path='" + entry.Path + "'], '" + name + "', '" + schemaClassName + "')");
            return(result);
        }
        public static DirectoryEntry Invoke(DirectoryEntry entry, string methodName, params object[] args)
        {
            if (OSTrace.LogLevel.TraceVerbose)
            {
                OSTrace.Info("Start OSDirectoryEntry.Invoke([DirectoryEntry.Path='" + entry.Path + "'], '" + methodName + "', " + args + ")\n" + GetCallingStack());
            }

            DirectoryEntry result = null;

            try {
                result = (DirectoryEntry)entry.Invoke(methodName, args);
            } catch (Exception e) {
                OSTrace.Error("OSDirectoryEntry.Invoke([DirectoryEntry.Path='" + entry.Path + "'], '" + methodName + "', " + args + ")", e);
                throw e;
            }
            OSTrace.Info("Finish OSDirectoryEntry.Invoke([DirectoryEntry.Path='" + entry.Path + "'], '" + methodName + "', " + args + ")");
            return(result);
        }
        public static DirectoryEntry FindChildren(DirectoryEntry entry, string name, string schemaClassName)
        {
            if (OSTrace.LogLevel.TraceVerbose)
            {
                OSTrace.Debug("OSDirectoryEntry.FindChildren([DirectoryEntry.Path='{0}'], '{1}', '{2}')\n{3}", entry.Path, name, schemaClassName, GetCallingStack());
            }

            DirectoryEntry result = null;

            try {
                result = entry.Children.Find(name, schemaClassName);
            } catch (Exception e) {
                OSTrace.Error(string.Format("OSDirectoryEntry.FindChildren([DirectoryEntry.Path='{0}'], '{1}', '{2}')", entry.Path, name, schemaClassName), e);
                throw e;
            }

            OSTrace.Debug("Finish OSDirectoryEntry.FindChildren([DirectoryEntry.Path='{0}'], '{1}', '{2}')", entry.Path, name, schemaClassName);
            return(result);
        }