Adding Debug and Release References to Visual Studio Projects

Posted by Al on Sat, November 12 at 11:40:42 in C#

visual studio image
A minor annoyance with Visual Studio, is that it doesn’t allow you to include conditional references to Debug and Release versions of class libraries.  For example, if you have a project and you add a reference to a class library by right-clicking on the ‘Reference’ folder and selecting the ‘Add Reference…’ menu item, the version of the library that you choose, be it Debug or Release, will always be used regardless of whether you choose to build a Debug or Release version of your project.

There is a way around this.  First, add your reference to the class library using Visual Studio as you normally would.  Next, close your project and open your project file (*.csproj) using the source code editor.  Find the location of the reference you just added and replace the ‘Debug’ or ‘Release’ portion of the string with ‘$(Configuration)’.

Code after the break…

Read more

WPF Dependency Properties

Posted by Al on Sun, April 17 at 14:50:04 in C#

Traditionally, a property in .NET is a member variable with associated get and set methods that are used to read and write its value.

Ex.Traditional .NET Property

private int mEmployeeRating;

 

public int EmployeeRating

{

    get { return mEmployeeRating; }

    set { mEmployeeRating = value; }

}

WPF introduces the concept of a dependency property, which also uses get and set methods to read and write its value, but instead of storing the property value in a member variable, instead stores its value in a dictionary maintained by the property system in WPF.  In other words the back store for a traditional property is a member variable and the back store for a dependency property is a dictionary maintained by the property system.  This gives the property system the ability to manipulate the value returned by the property.

So why would you want to use a dependency property?

Dependency properties give you additional functionality that traditional properties do not supply.  Dependency properties allow you to:

  • Set the property value using resources.
  • Bind the property to a data source, so that property value gets update dynamically whenever the data source value changes.
  • Set the property value …

    Read more

Programatically Shutdown of a Computer without Administrative Privileges

Posted by Al on Sat, January 30 at 12:42:53 in C#

I recently fixed a bug at work where one of our applications could not shutdown the computer it was running on when the user logged out.  It turned out the problem was introduced when we recently modified the software to run under a non-administrator account.  We had made the system more secure, but the program no longer had sufficient privileges to shutdown the computer.  The fix for this was to have the software invoke the shutdown program using an administrator account.  Since the solution might be useful to someone else, I’m posting the code that I implemented.

Code appears after the break…

Read more