Click here to Skip to main content
15,891,136 members
Articles / Desktop Programming / WPF

Easy Navigation in Windows Explorer and Total Commander

Rate me:
Please Sign up or sign in to vote.
4.88/5 (12 votes)
30 Oct 2011MIT7 min read 45K   675   35  
Easy navigation in Windows Explorer and Total Commander.
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;

namespace NavigationAssistanceSample
{
    /// <summary>
    /// Implements a manager for total commander windows.
    /// </summary>
    public class TotalCommanderManager : INavigatorManager
    {
        private readonly string _totalCommanderPath;

        public TotalCommanderManager(string totalCommanderPath)
        {
            _totalCommanderPath = Path.GetFullPath(totalCommanderPath);
        }

        public bool IsNavigator(ApplicationWindow hostWindow)
        {
            uint hostProcessId;
            GetWindowThreadProcessId(hostWindow.Handle, out hostProcessId);

            Process hostProcess = Process.GetProcessById((int)hostProcessId);

            string executablePath = null;
            try
            {
                executablePath = hostProcess.MainModule.FileName;
            }
            catch (Exception)
            {
                //This may happen if hostProcess is not TotalCommander (e.g. Windows Explorer)
            }

            //Ignore case is not very accurate, but should prevent possible case mistakes
            return string.Equals(executablePath, _totalCommanderPath, StringComparison.InvariantCultureIgnoreCase);
        }

        public INavigator GetNavigator(ApplicationWindow hostWindow)
        {
            return new TotalCommander(hostWindow, _totalCommanderPath, false);
        }

        public INavigator CreateNavigator()
        {
            return new TotalCommander(null, _totalCommanderPath, true);
        }

        [DllImport("user32.dll", SetLastError = true)]
        public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions