FindControls for MasterPages

While working on an ASP.NET Website with a Master Page, I found a need for a function that could return all the dynamically created controls of a specific type. For instance, when you create a page full of textboxes, you’d like to be able to get/set their values. If you have ever worked with MasterPages, you’ll know how annoying it is to modify dynamically generated controls, since each control’s ID is modified to match the ContentPlaceHolder it resides in.

Below is the code for finding a series of controls based on a parent control (like a ContentPlaceHolder, or a Table)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;

/// <summary>
/// Extension Methods
/// </summary>
public static class Extensions
{
    #region Find Controls

    /// <summary>
    /// Finds a series of controls recursively
    /// </summary>
    /// <typeparam name="T">where T is of type System.Web.UI.Control</typeparam>
    /// <param name="parent">The parent control to search</param>
    /// <returns>A list of filtered controls based on a supplied type and predicate</returns>
    public static List<T> FindControls<T>(this System.Web.UI.Control parent) where T : System.Web.UI.Control {
        List<T> foundControls = new List<T>();
        FindControls<T>(parent, ref foundControls, null);
        return foundControls;
    }
    
    /// <summary>
    /// Finds a series of controls recursively, based on a predicate
    /// </summary>
    /// <typeparam name="T">where T is of type System.Web.UI.Control</typeparam>
    /// <param name="parent">The parent control to search</param>
    /// <param name="predicate">Filter returned controls based on a supplied predicate</param>
    /// <returns>A list of filtered controls based on a supplied type and predicate</returns>
    public static List<T> FindControls<T>(this System.Web.UI.Control parent, Func<T, bool> predicate) where T : System.Web.UI.Control {
        List<T> foundControls = new List<T>();
        FindControls<T>(parent, ref foundControls, predicate);
        return foundControls;
    }

    /// <summary>
    /// Finds a series of controls recursively, based on a predicate
    /// </summary>
    /// <typeparam name="T">where T is of type System.Web.UI.Control</typeparam>
    /// <param name="parent">The parent control to search</param>
    /// <param name="foundControls">A list of filtered controls based on a supplied type and predicate</param>
    /// <param name="predicate">Filter returned controls based on a supplied predicate</param>
    private static void FindControls<T>(System.Web.UI.Control parent, ref List<T> foundControls, Func<T, bool> predicate) where T : System.Web.UI.Control {
        if (parent == null) return;
        
        foreach (Control c in parent.Controls) {
            // Check this Control
            if (c is T && !c.GetType().IsSubclassOf(typeof(T))) {   // The Control isn't a Subclass of T (meaning, it's exactly type T)   
                if (predicate == null ? true : predicate((T)c)) {   // If the predicate isn't null, check the predicate's return values as well
                    foundControls.Add((T)c);                        // Control must be valid.
                }
            }
            
            // Check child Controls
            if (c.Controls.Count > 0)
                FindControls<T>(c, ref foundControls, predicate);
        }
    }

    #endregion Find Controls
}

It’s used like this: assume you have a list of TextBoxes that you’d ID’d as:
ID = “tbDynamic_23153″,
ID = “tbDynamic_23154″,
… etc …

that correspond to records in a database with ID=23153, ID=23154, etc; however, you also have design-time controls like “tbUserName” and “tbPassword”. If you want only the textboxes that start with “tbDynamic_”, then you’d do the following:

// Get the ContentPlaceHolder that the controls reside in...
ContentPlaceHolder cphContent = Master.FindControl("cphContent") as ContentPlaceHolder;

// Get all Textboxes that start with "tbDynamic_"...
List<TextBox> tbs = cphContent.FindControls<TextBox>(x => x.ID.StartsWith("tbDynamic_"));

// Then you can enumerate your database records and populate the textboxes...
for (var r in QueriedDatabaseRecords) {
    var tb = tbs.FirstOrDefault(x => x.ID = string.Format("tbDynamic_{0}", r.ID));
    
    if (tb != null)
        tb.Text = r.PropertyValue
}

It would be just as simple to get all CheckBoxes or TableRows. And it can be used when there isn’t a MasterPage, as well.

Quick update on ‘Hijinks at work’

Okay, so here’s an update on the Hijinks at work post.

The mark, who didn’t lock his computer yesterday, came into work this morning and started up his computer. I had the MMR app set in his startup folder, so when he logged in, the mouse started jumping every few seconds in a random direction. All of us in the room resisted the urge to laugh maniacally.

But then we noticed that he hadn’t tried to fix the problem yet. He acted oblivious to it. Only after about twenty minutes did he finally start to try and solve it. Now, the first thing I would have done was check for a remote session in VNC, and then the task manager for a rogue program. The mark, however, went to the control panel, and starting with the mouse setting, attempted to ‘fix it’.

At this point, the rest of us started sending emails to each other, since only I had line-of-sight to this guy’s monitor, so I could keep them informed. I watched as he searched through settings windows trying to solve it. He even picked up his mouse and shook it.

And then he shut down his computer, and left to go do other work.

Mission accomplished! And even better, since he didn’t solve it, it’ll happen again when he gets back.

Hijinxs at work

We have a co-worker who doesn’t understand that he’s got to lock his computer when he steps away from it, so we all decided to play a prank on him. Of course, there’s the standard “tape over laser under mouse” maneuver. Then there’s the “change his desktop backgrounds” or “change his Windows sounds” tactic. Mine was a bit more devious.

Below is the source code for a tiny program to run in the background, detectable only through task manager, that randomly moves the mouse pointer.

(Note: I use the .settings file because I can change the settings later to fine-tune them)

And here’s the Program.cs code. Create a Windows Form application (this is the important part – don’t create a Console app!), then delete the form and replace the Program.cs code with the code below. Compile and enjoy!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;

namespace mmr_e12
{
    /// <summary>
    /// Move Mouse Randomly Event 2012 Application
    /// </summary>
    static class Program
    {
        #region Settings

        /// <summary>
        /// The update speed for the Timer
        /// </summary>
        static int _MaxInterval = 2000;

        /// <summary>
        /// The maximum distance from the initial mouse position
        /// </summary>
        static int _MouseRadius = 100;

        /// <summary>
        /// Whether or not to use smooth mouse transitions
        /// </summary>
        static bool _UseSmoothness = true;

        /// <summary>
        /// The duration in seconds for the smooth mouse transition
        /// </summary>
        static double _SmoothnessDuration = 0.1;

        /// <summary>
        /// The perceived speed of the smooth transition
        /// (if Frame Time = 10ms, Perceived Speed = 100.0)
        /// </summary>
        static double _PerceivedSpeed = 500.0;

        #endregion Settings
        
        //**********************************



        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            try {
                // Create a Random Number Generator
                Random r = new Random();

                // Create a Windows Form Timer
                System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

                // Set the initial Interval low to get it going.
                timer.Interval = 100;

                // Set the Tick Event to update the mouse position
                timer.Tick += (sender, args) => {
                    
                    // Randomly determine change in position
                    var dx = r.Next(0, _MouseRadius * 2) - _MouseRadius;
                    var dy = r.Next(0, _MouseRadius * 2) - _MouseRadius;
                    
                    // Randomly determine next update interval
                    var z = (int)(_MaxInterval * r.NextDouble());
                    
                    // Prevent zero for update interval
                    if (z <= 0) z = 1;

                    // Move the cursor
                    if (_UseSmoothness)
                        SmoothMouseMove(dx, dy, _SmoothnessDuration);
                    else
                        Cursor.Position = new Point(Cursor.Position.X + dx, Cursor.Position.Y + dy);

                    // Update the Interval to increase the randomness
                    timer.Interval = z;
                };

                // Start the Timer
                timer.Start();

                // Run the Application Thread
                Application.Run();
            }
            catch (Exception Error) {
                // Show Error - no fun if failed!
                MessageBox.Show(string.Format("{0}", Error), "Application Error");
                
                // Exit application thread.
                Application.Exit();
            }
        }

        /// <summary>
        /// Smoothly transition the mouse pointer between it's current position and it's new position
        /// </summary>
        /// <param name="dx">Change in X Position</param>
        /// <param name="dy">Change in Y Position</param>
        /// <param name="durationInSeconds">The length of the transition time.</param>
        static void SmoothMouseMove(int dx, int dy, double durationInSeconds) {
            
            // Check for divide-by-zero & transition time
            if (!(durationInSeconds > 0))
                throw new ArgumentException("Transition Duration must be greater than 0.", "durationInSeconds");

            // Number of frames to "render"
            double frames = durationInSeconds * _PerceivedSpeed;

            // Length of pause time between frames
            int frameSleepLength = (int)((durationInSeconds / frames) * _PerceivedSpeed);

            // Create the change vector
            PointF vector = new PointF((float)(dx / frames), (float)(dy / frames));
            
            // Get the initial mouse position
            PointF mousePos = Cursor.Position;

            // Move through each frame of animation
            for (int i = 0; i < frames; i++) {
                
                // Set the new Cursor Position
                Cursor.Position = new Point((int)(mousePos.X += vector.X), (int)(mousePos.Y += vector.Y));

                // Sleep to transition over perceived time
                Thread.Sleep(frameSleepLength);
            }

        }
    }

    
}

Sending Wake-on-LAN Magic Packets from C#

I present, with little introduction, the MACAddress class. Within you will find simple test and conversion methods, with a single, simple to use method for sending a WOL ‘Magic’ Packet.

 

// MACAddress Class (Sending WOL 'Magic' Packets)
// Written by John Storer II (Feb 20, 2012)
//
// Feel free to use/modify this code as you wish, without liability.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace SendWOLPacket
{
    public class MACAddress
    {
        /// <summary>
        /// Test a MACAddress byte Array.
        /// </summary>
        /// <param name="macAddress"></param>
        /// <returns></returns>
        public static bool Test(byte[] macAddress) {
            if (macAddress == null) return false;
            if (macAddress.Length != 6) return false;

            return true;
        }

        /// <summary>
        /// Test a MACAddress string.
        /// </summary>
        /// <param name="macAddress"></param>
        /// <returns></returns>
        public static bool Test(string macAddress) {
            var valid_chars = "0123456789ABCDEFabcdef";

            if (string.IsNullOrEmpty(macAddress)) return false;
            if (macAddress.Length != 12) return false;

            foreach (var c in macAddress) {
                if (valid_chars.IndexOf(c) < 0) {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// Parse a MACAddress string into a byte array.
        /// </summary>
        /// <param name="macAddress"></param>
        /// <returns></returns>
        public static byte[] Parse(string macAddress) {
            byte[] mac = new byte[6];

            if (!Test(macAddress))
                throw new ArgumentException(
                    "Invalid MACAddress string.",
                    "macAddress",
                    null);

            for (var i = 0; i < 6; i++) {
                var t = macAddress.Substring((i * 2), 2);
                mac[i] = Convert.ToByte(t, 16);
            }

            return mac;
        }

        /// <summary>
        /// Attempt to parse a MACAddress string
        ///   without throwing an Exception.
        /// </summary>
        /// <param name="macAddress"></param>
        /// <param name="Address"></param>
        /// <returns></returns>
        public static bool TryParse(string macAddress, out byte[] Address) {
            try {
                Address = Parse(macAddress);
                return true;
            }
            catch {
                Address = null;
                return false;
            }
        }

        /// <summary>
        /// Convert a byte array MACAddress to a string.
        /// </summary>
        /// <param name="macAddress"></param>
        /// <returns></returns>
        public static string ToString(byte[] macAddress) {
            if (!Test(macAddress))
                throw new ArgumentException(
                    "Invalid MACAddress byte array.",
                    "macAddress",
                    null);

            return BitConverter.ToString(macAddress).Replace("-", "");
        }


        /// <summary>
        /// Sends a Wake-On-LAN 'magic' packet to
        ///   the specified MACAddress string.
        /// </summary>
        /// <param name="macAddress"></param>
        public static void SendWOLPacket(string macAddress) {

            if (!Test(macAddress))
                throw new ArgumentException(
                    "Invalid MACAddress string.",
                    "macAddress",
                    null);
            
            byte[] mac = Parse(macAddress);

            SendWOLPacket(mac);
        }

        /// <summary>
        /// Sends a Wake-On-LAN 'magic' packet to
        ///   the specified MACAddress byte array.
        /// </summary>
        /// <param name="macAddress"></param>
        public static void SendWOLPacket(byte[] macAddress) {

            if (!Test(macAddress))
                throw new ArgumentException(
                    "Invalid MACAddress byte array.",
                    "macAddress",
                    null);

            // WOL 'magic' packet is sent over UDP.
            using (UdpClient client = new UdpClient()) {

                // Send to: 255.255.255.0:40000 over UDP.
                client.Connect(IPAddress.Broadcast, 40000);

                // Two parts to a 'magic' packet:
                //     First is 0xFFFFFFFFFFFF,
                //     Second is 16 * MACAddress.
                byte[] packet = new byte[17 * 6];

                // Set to: 0xFFFFFFFFFFFF.
                for (int i = 0; i < 6; i++) {
                    packet[i] = 0xFF;
                }

                // Set to: 16 * MACAddress
                for (int i = 1; i <= 16; i++) {
                    for (int j = 0; j < 6; j++) {
                        packet[i * 6 + j] = macAddress[j];
                    }
                }

                // Send WOL 'magic' packet.
                client.Send(packet, packet.Length);
            }
        }

    }
}

Using this code is as simple as (assuming you have a form, a button named bSend and a text box named tbMACAddress):

private void bSend_Click(object sender, EventArgs e) {
    try {
        MACAddress.SendWOLPacket(tbMACAddress.Text);

        MessageBox.Show(
            string.Format("Packet Sent to '{0}'", tbMACAddress.Text));
    }
    catch (Exception Error) {
        MessageBox.Show(
            string.Format("Error:\n\n{0}", Error.Message), "Error");
    }
            
}

Why write a class to send WOL Packets? Mainly because I needed to wake up a PC at home remotely, if I really needed it. Also, someone here at work said, “I need to send WOL to this laptop, but I don’t have the Altiris Console handy,” to which I replied:

Challenge Accepted

Because who doesn't like a good challenge?

Folders, files and poorly described exceptions

You know, sometimes exceptions returned in C# are so irritatingly vague or indirect that it takes longer than I’d like to solve the problem.

For example, I’ve been attempting to loop through a large set of directories on a network share, for a list of users in Active Directory. I have both an File/Folder indexing library, and an Active Directory library to help speed up development.

foreach (User Account in Accounts) {
    if (!Directory.Exists(Account.HomeDirectory)) continue;

    List<DirectoryInfo> folders = new List<DirectoryInfo>();
    List<FileInfo> files = new List<FileInfo>();

    Folders.Index(
        new DirectoryInfo(Account.HomeDirectory),
        ref folders,
        ref files);

    files.Reverse();
    folders.Reverse();

    foreach (FileInfo fi in files) {
        if (fi.Exists) fi.Delete();
    }

    foreach (DirectoryInfo di in folders) {
        if (di.Exists) di.Delete(true);
    }
}

Irritatingly, it kept returning a “Access is denied” error on the first folder that it attempted to delete. I, of course, have had not nearly enough sleep to deal with this sort of obscure issue, but I plowed on. I ran the code under a domain admin account; that didn’t fix it. I added the existence checks and a bit of debugging output to find where it was failing exactly.

Unable to force the issue, I checked the folder that it was failing on, and the folder was “root\My Videos”. Properties showed “Read-only for files in this folder”.

*facepalm*

Here’s the fixed code. Essentially it sets the FileAttributes of the files and folders it attempts to delete to FileAttributes.Normal, thereby bypassing the “Access is Denied” error (which IMHO should be a “Unable to delete – folder is read-only”!) and allowing the deletion.

foreach (User Account in Accounts) {
    if (!Directory.Exists(Account.HomeDirectory)) continue;

    List<DirectoryInfo> folders = new List<DirectoryInfo>();
    List<FileInfo> files = new List<FileInfo>();

    Folders.Index(
        new DirectoryInfo(Account.HomeDirectory),
        ref folders,
        ref files);

    files.Reverse();
    folders.Reverse();

    foreach (FileInfo fi in files) {
        fi.Attributes = FileAttributes.Normal;
        if (fi.Exists) fi.Delete();
    }

    foreach (DirectoryInfo di in folders) {
        di.Attributes = FileAttributes.Normal;
        if (di.Exists) di.Delete(true);
    }
}

I’m guessing that if I still have issues, I’ll probably have to take ownership of the files/folders to delete them. Not a great start to the day.

Update 1:

I found what looks to be a “Privilege Enabling” library. This is definitely something I shall be adding to my toolkit.

public static void TakeOwnership(DirectoryInfo di) {
    Process p = Process.GetCurrentProcess();

    using (new ProcessPrivileges.PrivilegeEnabler(p, Privilege.TakeOwnership)) {
        DirectorySecurity ds = di.GetAccessControl();
        ds.SetOwner(WindowsIdentity.GetCurrent().User);
        di.SetAccessControl(ds);
    }
}

Update 2:

Wow. After trying to take ownership as above, I still had difficulties with certain “RECYCLER” files/folders, and taking ownership apparently wasn’t enough. Here’s the full-on, brute-force, take-control method:

public static void TakeOwnership(ref DirectoryInfo di) {
    Process p = Process.GetCurrentProcess();
    SecurityIdentifier si = WindowsIdentity.GetCurrent().User;

    // Raise the process' access privileges.
    using (new ProcessPrivileges.PrivilegeEnabler(p, Privilege.TakeOwnership, Privilege.Security)) {
        // Get access rules.
        DirectorySecurity ds = di.GetAccessControl();

        // Take "full control" of everything.        
        FileSystemAccessRule fsaRule = new FileSystemAccessRule(
            si,
            FileSystemRights.Delete | FileSystemRights.Modify,
            InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
            PropagationFlags.None,
            AccessControlType.Allow);

        // Remove previous access rules.
        ds.PurgeAccessRules(si);

        // Add the new access rules.
        ds.AddAccessRule(fsaRule);

        // Take ownership
        ds.SetOwner(si);

        // Set the access rules to the object.
        di.SetAccessControl(ds);
    }
}