Get a List of Active Local Users

by adam 7. September 2010 23:08

The code below will return all users on the machine that are not marked as disabled.  Be sure to add a reference to System.DirectoryServices.

using System;
using System.Collections.Generic;
using System.DirectoryServices;
//... 
private const int UF_ACCOUNTDISABLE = 0x0002;

private List<DirectoryEntry> GetActiveMachineUsers()
{
    List<DirectoryEntry> returnValue = new List<DirectoryEntry>();

    DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);

    foreach (DirectoryEntry user in localMachine.Children)
    {
        if (user.SchemaClassName == "User")
        {
            if (((int)user.Properties["UserFlags"].Value & UF_ACCOUNTDISABLE) != UF_ACCOUNTDISABLE)
            {
                returnValue.Add(user);
            }
        }
    }

    return returnValue;
}

kick it on DotNetKicks.com

Tags: ,

.NET | C#

Determining if .NET CF ComboBox is Dropped Down

by adam 23. October 2009 15:44

The following snippet of code can be used to determine whether a Windows Mobile ComboBox is currently dropped down or not:

using Microsoft.WindowsCE.Forms;

...

private const int CB_GETDROPPEDSTATE = 0x0157;

private bool IsComboBoxDroppedDown(ComboBox comboBox)
{
    Message comboBoxMessage = Message.Create(comboBox.Handle, CB_GETDROPPEDSTATE, 
         IntPtr.Zero, IntPtr.Zero);
    MessageWindow.SendMessage(ref comboBoxMessage);

    return (comboBoxMessage.Result != IntPtr.Zero);
}


kick it on DotNetKicks.com

Tags: ,

.NET | C#

Visual Studio 2010 and .NET 4 Beta 2 now available to MSDN subscribers

by adam 19. October 2009 10:43

Microsoft has made Visual Studio 2010 and .NET 4 beta 2 available today to MSDN subscribers with general availability on Wednesday (10/21).

Download the beta from: http://msdn.microsoft.com/en-us/vstudio/dd582936.aspx

There are several new features available which you can read about at the following links:

Scott Guthrie has written several great posts detailing new features in Visual Studio 2010:
http://weblogs.asp.net/scottgu/archive/2009/08/25/vs-2010-and-net-4-series.aspx

What's new in ASP.NET 4.0 (Microsoft Whitepaper):
http://www.asp.net/learn/whitepapers/aspnet40/

Learning Resources for C# 4.0 and .NET 4.0:
http://bogdanbrinzarea.wordpress.com/2009/04/24/learning-net-40-new-features/

kick it on DotNetKicks.com

Tags: , ,

Visual Studio | .NET

GroupBy and Average with Lambda Expressions

by adam 7. October 2009 11:11

I was recently tasked with generating a string of averages based on a list of business objects.  The business object has a string property called VehicleName and a decimal property called StandardMpg.  The averages needed to be calculated per VehicleName, so this is to be what I group on.  This took me a few minutes to wrap my mind around, but once I got it, it seemed fairly simple. I thought it would be worth sharing with the rest of you:

string averages = string.Format("Avg: {0}",
    string.Join(" | ", gasMileageList
        .GroupBy(x => x.VehicleName)
        .Select(c => string.Format("{0} {1} MPG", c.Key, Math.Round(c.Average(x => x.StandardMpg), 1))).ToArray()));

The output will look something like: "Avg: bmw 21 MPG | kia 30 MPG"

Another option would be to dump the averages out to a list of objects, instead of a string.  To do this you can do:

private class MileageAverage
{
public string VehicleName { get; set; }
public decimal Average { get; set; }
}

...

var averages = gasMileageList
    .GroupBy(x => x.VehicleName)
    .Select(c => new MileageAverage { VehicleName = c.Key, Average = c.Average(x => x.StandardMpg) });


kick it on DotNetKicks.com

Tags: ,

C#

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About Adam

Adam Plocher

Adam Plocher is a professional Software Engineer in the Sacramento area. He specializes in Microsoft and .NET based development technologies.

Twitter: Adam Plocher LinkedIn: Adam Plocher