Dailycode.info

Short solution for short problems

Spam in Blogengine.Net 1.5

I'm gettings attacked by spam bots, some times hundreds of SPAM comment each day.

So I was looking for aniti-spam measurements. Lack of time, I tried only 1 solution, but it failed, so I just tried the following:

I added an image with some text, this is always the same text. And the user has to fill in this text. My guess is it will keep the bots away, since they will not write a special bot only for this image and this blog. I put a regular expression field on the textbox checking on the text. So it only takes 3 or 4 lines of code to implement this:

1. Add an image with custom text

2. Add a textbox

3. Set a required field validator on the textbox

4. Set a regular expression validator on the textbox, checking on your text.  

After some waiting, the spam bots still got through, even if I removed the image. So my guess is that they look for the validators. So I will have to do the check in the code behond.


Calculate the standard deviation

These functions help you to calculate the standard deviation for a list of values. The flow is simple, first calculate the average, then the total variance and return the square root of the total variance divided by the number of values.

/// <summary>

/// Returns the standard deviation for a row of values

/// </summary>

/// <param name="data">Row of values to calculate the standard deviation from</param>

/// <returns>STDev</returns>

public static double StandardDeviation(double[] data)

{

    double ret = 0;

    double DataAverage = 0;

    double TotalVariance = 0;

    int Max = 0;

 

    try

    {

 

        Max = data.Length;

 

        if (Max == 0) { return ret; }

 

        DataAverage = Average(data);

 

        for (int i = 0; i < Max; i++)

        {

            TotalVariance += Math.Pow(data[i] - DataAverage, 2);

        }

 

        ret = Math.Sqrt(SafeDivide(TotalVariance, Max));

 

    }

    catch (Exception) { throw; }

    return ret;

}

 

 

/// <summary>

/// Divides 2 numbers, if error occurs, return 0, if one of the numbers = 0, returns 0

/// </summary>

/// <param name="value1">Value 1</param>

/// <param name="value2">Value 2</param>

/// <returns>Result or in case of error 0</returns>

public static double SafeDivide(double value1, double value2)

{

 

    double ret = 0;

 

    try

    {

 

        if ((value1 == 0) || (value2 == 0)) { return ret; }

 

        ret = value1 / value2;

 

    }

    catch { }

    return ret;

}

 

 

/// <summary>

/// Calculated the average for a list of values

/// </summary>

/// <param name="data">Values</param>

/// <returns>Average</returns>

public static double Average(double[] data)

{

    //double ret = 0;

    double DataTotal = 0;

 

    try

    {

 

        for (int i = 0; i < data.Length; i++)

        {

            DataTotal += data[i];

        }

 

        return SafeDivide(DataTotal, data.Length);

 

    }

    catch (Exception) { throw; }

}


Get file location from a user

I made a small function to reduce the code you write when you want to prompt the user for a file location.

Here the result:

public static string GetLocationFromUser(string text, string defaultExt, string filterExp)

{

    // Select a file     OpenFileDialog dlgOpenFile = new OpenFileDialog();  

    dlgOpenFile.Title = text;

    if (!String.IsNullOrEmpty(defaultExt))

    {

        dlgOpenFile.DefaultExt = defaultExt;

        if (String.IsNullOrEmpty(filterExp))

        {

            dlgOpenFile.Filter = "(*." + defaultExt + ")|*." + defaultExt + "|All files (*.*)|*.*";

        }

    }

    if (!String.IsNullOrEmpty(filterExp))

    {

        dlgOpenFile.Filter = "comma separated files (*.csv)|*.csv|All files (*.*)|*.*";

        dlgOpenFile.FilterIndex = 1;

    }

    dlgOpenFile.RestoreDirectory = true;

    //Only when OK has been clicked then execute next code ...

    if(dlgOpenFile.ShowDialog() == DialogResult.OK)

    {

        return dlgOpenFile.FileName;

    }

    else

    {

        return String.Empty;

    }

}

 

 

 


Randomly shuffle an IList

Here some code to shuffle an IList randomly:

/// <summary>

/// Function to randomly shuffle an IList

/// </summary>

/// <param name="list">The IList to shuffle</param>

public static void ShuffleList(IList list)

{

    Random rnd = new Random();

 

    for (int i = list.Count - 1; i > 1; --i)

    {

        int n = rnd.Next(i - 1);

        object t = list[i];

        list[i] = list[n];

        list[n] = t;

    }

 

}


How to use the Find method with a generic list

You will have to implement a delegate method that will handle the comparison.

In this example I'm looking for a class of type Organ in a generic list of type List<Organ>. The match will be the organ guid.

 
Organ result = organsIn.Find(
    delegate(Organ or)
    {
        return or.OrganGuid == "guid" ? true : false;
    }
    );

This method will return the organ if found, or else null if not found.