Converting a mapped drive letter to a network path using C#

Occasionally you might have the need to convert a mapped drive letter to a UNC or network path. For example, a drive letter such as  “Z” might be mapped to a network share:

image

In this example, the “Z” drive is mapped to a “Personal” folder on a server named “Home”.

If you want to store for example a UNC path to a file rather than a drive letter, you’ll need to convert the “Z” drive to the corresponding UNC root path. Here’s the C# code I wrote to do this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace WiredPrairie.Samples
{
    public static class Pathing
    {
        [DllImport("mpr.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        public static extern int WNetGetConnection(
            [MarshalAs(UnmanagedType.LPTStr)] string localName, 
            [MarshalAs(UnmanagedType.LPTStr)] StringBuilder remoteName, 
            ref int length);
        /// <summary>
        /// Given a path, returns the UNC path or the original. (No exceptions
        /// are raised by this function directly). For example, "P:\2008-02-29"
        /// might return: "\\networkserver\Shares\Photos\2008-02-09"
        /// </summary>
        /// <param name="originalPath">The path to convert to a UNC Path</param>
        /// <returns>A UNC path. If a network drive letter is specified, the
        /// drive letter is converted to a UNC or network path. If the 
        /// originalPath cannot be converted, it is returned unchanged.</returns>
        public static string GetUNCPath(string originalPath)
        {
            StringBuilder sb = new StringBuilder(512);
            int size = sb.Capacity;

            // look for the {LETTER}: combination ...
            if (originalPath.Length > 2 && originalPath[1] == ':')
            {
                // don't use char.IsLetter here - as that can be misleading
                // the only valid drive letters are a-z && A-Z.
                char c = originalPath[0];
                if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
                {
                    int error = WNetGetConnection(originalPath.Substring(0, 2), 
                        sb, ref size);
                    if (error == 0)
                    {                        
                        DirectoryInfo dir = new DirectoryInfo(originalPath);

                        string path = Path.GetFullPath(originalPath)
                            .Substring(Path.GetPathRoot(originalPath).Length);
                        return Path.Combine(sb.ToString().TrimEnd(), path);
                    }
                }
            }
            
            return originalPath;
        }
    }
}

All of the magic takes place in a Windows API function call, WNetGetConnection.

I’m using this function so that I can verify that given two file names, I can be assured that they are pointing to the same physical location. (Is “Z:\Backups\Backup1.zip” the same as “\\home\Personal\Backups\Backup1.zip”).

How do you do search and filtering?

If you’re creating a user interface for searching, what’s your approach?

Something simple?

image

What happens after the results is presented? Can the user filter and refine the results to find exactly what they want? Are they forced to use an "advanced" mode? Or, scroll through pages upon pages of results hoping to find the right match?

If you’re interested in making a better search, read Pattern: Refining Search from Looks Good Works Well.

imageOne of the most recent filtering searches I’ve used is from NewEgg. I was trying to find a new quiet graphics card to replace the hair drier model I’ve currently got installed. It’s clear that NewEgg is attempting to cater to many types of users. They have three different types of searches! The one with the most potential, but doesn’t necessarily follow all of the suggestions in the blog post linked above is the Guided Search. What I like about this search is that it provides feedback before I make a selection. For example, in the screen shot, I can see that there are 6 graphics cards that are priced between $10 – $25. If I click on that link, the list is updated with a new set of choices.

imageThe Advanced Search takes the old school approach, one where you can almost imagine the SQL query that is constructed when the user hits the "Search" button. My simple complaint with this one is that my searches usually end up with no results as I have "over-specified" my requirements. I won’t know this until I hit the search button though. Worse, I don’t often know what to remove or simply in the search. It’s too hit and miss.

image Finally, they offer a "Power Search." This search expands the drop-down list idea and allows the user to multiple select from each category. Again, until the search is completed, there’s no sense of whether a search will generate any results. Hardly a spectacular user experience.

Although not perfect, the Guided Search is the most discoverable and friendly search that NewEgg offers to the general consumer. It uses Ajax nicely to update the results. One major challenge with the Guided Search from a user perspective is that the list is constantly changing, which means that the user must reorient themselves after each click.

Do you have any favorite search and filtering experiences you’d care to share? Leave a comment.

Adobe announces, AIR, Flex 3, Open Source, and the kitchen sink

Adobe made a handful of announcements early this morning:

The Flex team’s blog announcement has more details and a lot of links to get you started.

Is it just me, or are none of the demo applications very compelling?

image

The biggest change from "yesterday" is that it might be easier today to write a cross-platform application — as long as AIR/Flex supports what you want to do outside of the browser. I’d like Adobe to offer the "Express" edition of Flex Builder so I can build things for free, just like Microsoft does with Visual Studio.

Are you excited about this "AIR"? (Or Flex?)

The only car show my wife will watch with me…. TopGear

My wife will watch lots of shows that I like to watch, partly because she has similar tastes, but also because the shows I watch aren’t too awful (and there’s that whole marriage sharing thing …). One show I suggested a few months ago that looked like it might be interesting was the hour long show on BBC America, TopGear. It’s a car show. I could see her skepticism visibly when I announced it was something I wanted to watch together. "A car show?" Food shows, travel shows, dramas, even history shows she’ll watch, but a car show?

image

By the end of the first episode, after laughing and enjoying it thoroughly, she gave this show the "approved" nod and now looks for new episodes to show up on the DVR. Since we’re just catching up and watching some re-runs, we’ve been able to watch quite a few episodes recently.

Starting February 25th, 8pm et/pt, Top Gear’s new season begins to air on BBC America. Sure, they talk of cars that aren’t available in the states, but real car talk is only a small part of the show. The hosts make "car talk" fun.

Blue Iris Security Camera DVR Software for Windows

I’ve used Blue Iris for several months now to monitor a few security cameras we have installed around our house. It’s a brilliant piece of software that works as well as many software packages that cost four to ten times as much (trust me, I’ve experimented and tested packages that were $500 US).

The key features for me that I needed:

  • Runs as a Windows Service — even if no one is logged onto the computer, the application is still monitoring
  • Runs on Windows 2003 Server (or Windows Home Server).
  • Can send e-mail alerts when motion is controlled with a configurable options (like the maximum frequency of e-mails sent within a period).
  • Support of my IP based cameras.
  • Reasonable CPU usage (about 30-40% constant on my home server to monitor 4 cameras).
  • Continually updated — not a dead product
  • Remote access via a web page (the product does this well, but it’s not an attractive web page — just functional).
  • Priced competitively — this product is priced almost too low — at $49.95 for the unlimited package.
  • Stable — I haven’t had a single problem with the stability of the product in months.

image

It has tons of options for configuration:

image

image

Tons of settings for each individual camera:

image

Here’s the built in web application (it’s ActiveX or Java based):

image

It even works with my Airlink 250W wireless IP camera (with a few tricks):

image

Even though the main application user interface is not very "professional" — it’s a killer program and if you need something like this, I’d strongly recommend you consider it.

Windows only.