Archive for September 2011

Listing available Properties of an DirectoryEntry object

If you are messing with IIS in C# code, you can do quite a bit if you System.DirectoryServices and the DirectoryEntry object. This can be extended if you know the name of the items in a the DirectoryEntry’s property collection. You can get these as follows:

using System;
using System.Collections.Generic;
using System.DirectoryServices;

namespace FindIISLogWithCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> AvailableProperties = new List<string>();
            DirectoryEntry folderRoot = new DirectoryEntry("IIS://localhost/W3SVC");
            PropertyCollection pc = folderRoot.Properties;
            foreach (PropertyValueCollection p in pc)
            {
                AvailableProperties.Add(p.PropertyName);                
            }
            
            // Sort alphabetically
            AvailableProperties.Sort();                       
            foreach (String prop in AvailableProperties)
            {
                Console.WriteLine(prop);
            }
        }
    }
}

Of course, different DirectoryEntry objects can have different lists. However, this exact one created the following list on a Windows 2008 R2 64-bit Server with IIS installed and configured.

AccessFlags
AccessSSLFlags
AnonymousUserName
ApplicationDependencies
AppPoolId
AspDiskTemplateCacheDirectory
AuthFlags
DefaultDoc
DirBrowseFlags
DontLog
HttpCustomHeaders
HttpErrors
IIs5IsolationModeEnabled
KeyType
LogFileDirectory
LogPluginClsid
NTAuthenticationProviders
RedirectHeaders
ScriptMaps
SSIExecDisable
SslUseDsMapper
TraceUriPrefix
WebSvcExtRestrictionList

Java version of the sequential sum algorithm or n(n+1)/2

My first Android homework was to write a little program that would add sequential numbers such as 1 to 100. I call it a Sequential Sum, but I couldn’t find the exact mathematical name.

You can read more about this mathematical task here: Techniques for adding the numbers 1 to 100

In code, you could of course loop through 1 through 100 and += them into a value but there is a more efficient way and most everyone in my class mentioned that is what they did, so I guess I was unique in choosing this solution.

Or you could use the following equation that provides you the answer to get the sum of all numbers between 1 and the end value, n.

n(n+1)/2

Now what if you aren’t starting at 1? What if you want to add the numbers between 50 and 150? Well, this is easy. The last number before the start value of 50 is 49 or 50 – 1 = 49. We can get the value of 1 to 49 and subtract it from the value of 1 to 150.

n(n+1)/2 – s(s+1)/2

While this is for Android, it is basically just java and there is nothing Android specific here. Here is my class.

package org.jaredbarneck.cs6890;

import java.security.InvalidParameterException;

/**
 * @author Jared
 *
 */
public class SequentialSum {

	// Member variables
	private int _LowValue = 0;
	private int _HighValue = 0;

	// Constructors
	public SequentialSum(int inLowValue, int inHighValue) {
		SetHighValue(inHighValue);
		SetLowValue(inLowValue);
	}

	// Getters and Setters
	public int GetLowValue() {
		return _LowValue;
	}

	public int GetHighValue() {
		return _HighValue;
	}

	public void SetLowValue(int inValue) {
		if (inValue < 0)
			throw new InvalidParameterException(
					"Value must be greater than zero!");
		if (inValue > _HighValue)
			throw new InvalidParameterException(
					"High value must be lower than the high value!");

		_LowValue = inValue;
	}

	public void SetHighValue(int inValue) {
		if (inValue < 0)
			throw new InvalidParameterException(
					"Value must be greater than zero!");
		if (inValue < _LowValue)
			throw new InvalidParameterException(
					"High value must be greater than the low value!");

		_HighValue = inValue;
	}

	// Methods
	public int Sum() {

		int sumToSubtract = 0;
		if (_LowValue > 1) {
			int tmpVal = _LowValue - 1;
			sumToSubtract = tmpVal * (tmpVal + 1) / 2;
		}

		return (_HighValue * (_HighValue + 1) / 2) - sumToSubtract;
	}
}

So if I ever need to get the sum of all values between a sequential list of numbers, I can come back and use this class again.

Note: No, I don’t follow java syntax rules and no, I don’t plan too either. I don’t start anything with lower case that is not a local variable. My member variables start with an underscore _ and then an uppercase letter. Everything else starts with upper case. I don’t conform my style to a specific language’s style, instead I try to use the same style in all languages.

I’m starting a Masters in Computer Science with an Android and a Computer Security course

Utah State Online Masters of Computer ScienceFor those who don’t know already, I am preparing to start a Masters in Computer science. Yes, it is crazy…I am an now a USU aggie.

I can take four classes before I have to be officially accepted, and my first two classes are Android Development and Computer Security.  Both are great classes.

I don’t get to use MonoDroid from my Android class, so yes, I will be writing code in Java using Eclipse (gasp!).  By the way, I already know Java and I have used Eclipse plenty, even though I work in C#, every class in school I have taken had me code in Java and I have coded for Salesforce, which uses Eclipse and is Java-like.

My security class is to be written in C++ on an open source platform, so I will be putting my computer back into dual-boot with PC-BSD.  I may try MonoDevelop’s IDE for my C++ tasks or maybe KDevelop, or I may just go command line.

So expect a few java/Android posts and some C++ posts that are related to security.  However, I still work in C# and I still like BSD, so expect those posts too.

PC-BSD 9.2 Beta 2 available for testing – I just installed it

PC-BSD 9.2 Beta 2 is available for testing and I just installed it. This is probably the best desktop OS that is not Windows, Mac, or Linux.

The installer was already a nice easy GUI, so I didn’t expect many changes and it is still nice and easy to use, and just works.

The installer does present new features about PC-BSD 9.2, in that you can choose from a list of Desktop Managers. Before KDE was it during install and if you wanted something else, you could change it later, but now you have a nice list of GNOME, KDE, and others.

I chose KDE because I usually use KDE or Fluxbox and when I have plenty of system power, I go with KDE and this system is on a beefy VM.

I did miss seeing Firefox as an installable application during install? Not a big deal, as I will install it later…but I was used to it being there.

The install went flawlessly.

Setting up the Video Card still is a screen. It would be best to have it just work and not even have the screen at all. But it still prompts me and I still have to do it. I would like it to detect that I am on VMWare and just install the VMWare video driver for me or if I am on hardware, just install the driver I need.

There is a nice “tip” screen post login.

Branding is very important! AppCafe is probably one of the first times I have ever seen PC-BSD really do something that I consider a good brand. I have an opinion that the PC-BSD, which is 5 letters where you just say the letters name like an acronym, is a horrible brand and should be changed as soon as possible. However, AppCafe is great. It goes with the whole App Store idea that is trendy currently. Good move!

I quickly found Firefox and Chrome in the AppCafe. The search feature is great, otherwise they were under the “Web” group. I was curious as to why Firefox was still version 4.0.1 as Firefox is on 6.x now. The one downside is that I would really like Firefox to load a lot faster.

Flash…Yes, it works with a beta note: In previous versions, flash has worked perfectly, and it will in the next release too. However, turns out that this beta has a bug where you have to run a command to enable flash…oops. That is why we have betas. Expect this to work in the final release as Flash has worked out of the box now for multiple releases.

For KDE, After installation, App icons are placed in a desktop folder, that is a transparent box on the screen, giving the system a sense of “flashiness”.

A lot of the default Apps that were installed, such as K3B, a DVD/CD burning software, and a video player and music player are all there.

I am probably going to install a GNOME only version as well and give it a look.

A snippet for Properties in a ViewModel

If you are using MVVM you probably should create a snippet very similar to the following to save time.

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Visual C#\propvm.snippet

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>propvm</Title>
			<Shortcut>propvm</Shortcut>
			<Description>Code snippet for property and backing field for a ViewModel that calls NotifyPropertyChanged.</Description>
			<Author>Jared Barneck</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>type</ID>
					<ToolTip>Property type</ToolTip>
					<Default>int</Default>
				</Literal>
				<Literal>
					<ID>property</ID>
					<ToolTip>Property name</ToolTip>
					<Default>MyProperty</Default>
				</Literal>
			</Declarations>
			<Code Language="csharp"><![CDATA[public $type$ $property$
	{
		get { return _$property$;}
		set 
		{ 
			_$property$ = value;
			NotifyPropertyChanged("$property$");
		}
	} private $type$ _$property$;
	$end$]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

WPF Binding to a property of a static class

Binding to a property of a static class is quite straight forward.

The binding string looks like this:

{x:Static s:MyStaticClass.StaticValue2}

For an example do this:

  1. Create a new folder in your project called Statics.
  2. Add the following class to the Statics folder.
    using System;
    
    namespace BindingToStaticClassExample.Statics
    {
        public static class MyStaticClass
        {
            static MyStaticClass()
            {
                Title = "Binding to properties of Static Classes";
                StaticValue1 = "Test 1";
                StaticValue2 = "Test 2";
                StaticValue3 = "Test 3";
            }
    
            public static String Title { get; set; }
            public static String StaticValue1 { get; set; }
            public static String StaticValue2 { get; set; }
            public static String StaticValue3 { get; set; }
        }
    }
    
  3. Add a reference to the BindingToStaticClassExample.Statics namespace. (See line 4.)
  4. Bind the title to the Title string value of MyStaticClass.
  5. Change the <Grid> to a <StackPanel> (not required just for ease of this example).
  6. Add TextBox elements and bind them to the two string values in MyStaticClass object.
    <Window x:Class="BindingToStaticClassExample.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:s="clr-namespace:BindingToStaticClassExample.Statics"
            Title="{x:Static s:MyStaticClass.Title}" Height="350" Width="525">
        <StackPanel>
            <TextBox Text="{x:Static s:MyStaticClass.StaticValue1}" />
            <TextBox Text="{x:Static s:MyStaticClass.StaticValue2}" />
            <!-- Not supported and won't work
            <TextBox>
                <TextBox.Text>
                    <Binding Source="{x:Static s:MyStaticClass.StaticValue3}" />
                </TextBox.Text>
            </TextBox>
            -->
        </StackPanel>
    </Window>
    

You now know how to bind to properties of a static class.

Cool Open Source Games you should contribute to (from Shinnok’s Rants)

Open Source games

Cool Open Source Games you should contribute to

I found this site about open source games. I didn’t know about most of these.

Time to download and play one.

If you have already played one, please comment and let me know how you liked the game.

Too bad the only one I have played is Extreme Tux Racer.