Skip to content

AD – Enumerate domains in the current forest

Enumerate domains in the current forest

public static ArrayList EnumerateDomains()
    ArrayList alDomains = new ArrayList();
    Forest currentForest = Forest.GetCurrentForest();
    DomainCollection myDomains = currentForest.Domains;

    foreach (Domain objDomain in myDomains)
    return alDomains;

just another line in the file Getting list of (sub-)domains from Active Directory with C#

split comma 2

publicstaticIEnumerable<string>SplitCSV(string csvString){var sb =newStringBuilder();
    bool quoted =false;foreach(char c in csvString){if(quoted){if(c =='"')
                quoted =false;else
                sb.Append(c);}else{if(c =='"'){
                quoted =true;}elseif(c ==','){
                yield return sb.ToString();
                sb.Append(c);}}}if(quoted)thrownewArgumentException("csvString","Unterminated quotation mark.");

    yield return sb.ToString();}

Split Comma

        public void StringTest()
            const string input = @"""Peter"",""Pan"",,""Hello, is it ok"",";
            var strings = Split(input);
            Assert.That(strings[0], Is.EqualTo("Peter"));
            Assert.That(strings[1], Is.EqualTo("Pan"));
            Assert.That(strings[2], Is.EqualTo(""));
            Assert.That(strings[3], Is.EqualTo("Hello, is it ok"));

        protected virtual string[] Split(string value)
            var regex = new Regex("(?:^|,)(\\\"(?:[^\\\"]+|\\\"\\\")*\\\"|[^,]*)");
            var collection = regex.Matches(value);
            var items = new string[collection.Count];
            var i = 0;
            foreach (Match match in collection)
                items[i++] = match.Groups[0].Value.Trim('"').Trim(',').Trim('"').Trim();
            return items;

windows service in C#

First approach with Windows Service is not easy..

Lot of time ago, i have written a c# service.

This is the logic of the Service class (tested, works fine):

namespace MyServiceApp
    public class MyService : ServiceBase
        private System.Timers.Timer timer;

        protected override void OnStart(string[] args)
            this.timer = new System.Timers.Timer(30000D);  // 30000 milliseconds = 30 seconds
            this.timer.AutoReset = true;
            this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_Elapsed);

        protected override void OnStop()
            this.timer = null;

        private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            MyServiceApp.ServiceWork.Main(); // my separate static method for do work

        public MyService()
            this.ServiceName = "MyService";

        // service entry point
        static void Main()
            System.ServiceProcess.ServiceBase.Run(new MyService());

I recommend to write your real service work in a separate static method (why not, in a console application…just add reference to it), for simplify debugging and cleaning service code.

Make sure the interval is enough, and write in log ONLY in OnStart and OnStop overrides.

Hope this help!



SQL Server Mail : Database Mail & SQL Mail

Database Mail is new in 2005 with some new features.


Fit a Flash SWF File to Any Screen Size

<!DOCTYPE html>
<html lang="en">

<meta charset="utf-8">

<title>untitled document</title>

html,body {
#swf-holder {
#swf-holder object {


<div id="swf-holder">