Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Resolving Symbolic References in a CodeDOM (Part 7)

, 2 Dec 2012 CDDL
Resolving symbolic references in a CodeDOM.
Nova.0.6.exe.zip
Mono.Cecil.dll
Nova.CLI.exe
Nova.CodeDOM.dll
Nova.Examples.exe
Nova.Studio.exe
Nova.Test.exe
Nova.UI.dll
Nova.0.6.zip
Nova.CLI
Properties
Nova.CodeDOM
CodeDOM
Annotations
Base
Comments
Base
DocComments
CodeRef
Base
List
Name
Base
Other
Simple
CompilerDirectives
Base
Conditionals
Base
Messages
Base
Pragmas
Base
Symbols
Base
Base
Interfaces
Expressions
AnonymousMethods
Base
Operators
Base
Binary
Arithmetic
Base
Assignment
Base
Bitwise
Base
Conditional
Relational
Base
Shift
Base
Other
Base
Unary
Base
Other
References
Base
GotoTargets
Base
Methods
Namespaces
Other
Properties
Types
Base
Variables
Base
Projects
Assemblies
Namespaces
References
Base
Statements
Base
Conditionals
Base
Exceptions
Generics
Constraints
Base
Iterators
Base
Jumps
Loops
Methods
OperatorDecls
Miscellaneous
Namespaces
Properties
Base
Events
Types
Base
Variables
Base
Parsing
Base
Properties
Rendering
Resolving
Utilities
Mono.Cecil
Reflection
Nova.Examples
Properties
Nova.Studio
Images
About.png
Configuration.png
EditCopy.png
EditCut.png
EditDelete.png
EditPaste.png
EditRedo.png
EditUndo.png
Error.png
Exit.png
FileNew.png
FileOpen.png
FileSave.png
FileSaveAll.png
FileSaveAs.png
Find.png
Help.png
Info.png
Logo.png
Options.png
Print.png
PrintPreview.png
Properties.png
Todo.png
Warning.png
Objects.ico
Properties
Settings.settings
Nova.Test
Properties
Nova.UI
CodeDOM
Annotations
Base
Comments
Base
DocComments
CodeRef
Base
List
Name
Base
Other
Simple
CompilerDirectives
Base
Conditionals
Base
Messages
Base
Pragmas
Base
Symbols
Base
Base
Expressions
AnonymousMethods
Base
Operators
Base
Binary
Arithmetic
Base
Assignment
Base
Bitwise
Base
Conditional
Relational
Base
Shift
Base
Other
Base
Unary
Base
Other
References
Base
GotoTargets
Base
Methods
Namespaces
Other
Properties
Types
Base
Variables
Base
Projects
Namespaces
References
Base
Statements
Base
Conditionals
Base
Exceptions
Generics
Constraints
Base
Iterators
Base
Jumps
Loops
Methods
OperatorDecls
Miscellaneous
Namespaces
Properties
Base
Events
Types
Base
Variables
Base
Properties
Resolving
Utilties
// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System;
using System.Collections.Specialized;
using System.IO;

namespace Nova.Utilities
{
    /// <summary>
    /// Helper methods for working with files and directories.
    /// </summary>
    public static class FileUtil
    {
        #region /* STATIC HELPER METHODS */

        /// <summary>
        /// Combine the two specified paths, removing any "..\" prefixes from the 2nd path,
        /// while removing corresponding subdirectories from the 1st path at the same time.
        /// Also, ensure that only a single '\' is used to combine the paths.
        /// </summary>
        public static string CombineAndNormalizePath(string path1, string path2)
        {
            if (path1.EndsWith(@"\"))
                path1 = path1.Substring(0, path1.Length - 1);
            while (path2.StartsWith(@"..\"))
            {
                path2 = path2.Substring(3);
                int path1LastSlash = path1.LastIndexOf('\\');
                if (path1LastSlash >= 0)
                    path1 = path1.Substring(0, path1LastSlash);
            }
            // Must make sure any "C:" has a '\' after it, because Path.Combine() won't add one in this case
            // (because 'C:../directory' is actually legal and useful).
            if (path1.EndsWith(":"))
                path1 += @"\";
            // Also, call GetFullPath() in order to eliminate any embedded '\..\'s between other directory names
            return Path.GetFullPath(Path.Combine(path1, path2));
        }

        /// <summary>
        /// Remove the specified common root path from the specified file name.
        /// </summary>
        public static string RemoveCommonRootPath(string fileName, string rootFileOrDirectory)
        {
            string currentLocation = Path.GetDirectoryName(rootFileOrDirectory) + @"\";
            return (fileName.StartsWith(currentLocation) ? fileName.Substring(currentLocation.Length) : fileName);
        }

        /// <summary>
        /// Determine a relative path that navigates from the first specified path to the second one.
        /// </summary>
        public static string MakeRelative(string fromPath, string toPath)
        {
            if (fromPath == null)
                throw new ArgumentNullException("fromPath");
            if (toPath == null)
                throw new ArgumentNullException("toPath");

            // If both paths are rooted, but aren't the same, just return 'toPath'
            if (Path.IsPathRooted(fromPath) && Path.IsPathRooted(toPath))
            {
                if (string.Compare(Path.GetPathRoot(fromPath), Path.GetPathRoot(toPath), true) != 0)
                    return toPath;
            }

            StringCollection relativePath = new StringCollection();
            string[] fromDirectories = fromPath.Split(Path.DirectorySeparatorChar);
            string[] toDirectories = toPath.Split(Path.DirectorySeparatorChar);
            int length = Math.Min(fromDirectories.Length, toDirectories.Length);

            // find common root  
            int lastCommonRoot = -1;
            for (int i = 0; i < length; i++)
            {
                if (string.Compare(fromDirectories[i], toDirectories[i], true) != 0)
                    break;
                lastCommonRoot = i;
            }
            if (lastCommonRoot == -1)
                return toPath;

            // add relative folders in from path
            for (int i = lastCommonRoot + 1; i < fromDirectories.Length; i++)
                if (fromDirectories[i].Length > 0)
                    relativePath.Add("..");

            // add folders in to path
            for (int i = lastCommonRoot + 1; i < toDirectories.Length; i++)
                relativePath.Add(toDirectories[i]);

            return StringUtil.ToString(relativePath, Path.DirectorySeparatorChar.ToString());
        }

        /// <summary>
        /// Get the base directory of the executing application, using the project directory if running in an output directory.
        /// </summary>
        public static string GetBaseDirectory()
        {
            string baseDirectory = Environment.CurrentDirectory + @"\";

            // If we're in an output directory, move up above it
            if (baseDirectory.EndsWith(@"\Debug\") || baseDirectory.EndsWith(@"\Release\"))
                baseDirectory = RemoveLastDirectory(baseDirectory);
            // Remove any '\bin\xxx' directories, or just '\bin'
            if (!baseDirectory.EndsWith(@"\bin\"))
            {
                string parentDirectory = RemoveLastDirectory(baseDirectory);
                if (parentDirectory.EndsWith(@"\bin\"))
                    baseDirectory = RemoveLastDirectory(parentDirectory);
            }
            else if (baseDirectory.EndsWith(@"\bin\"))
                baseDirectory = RemoveLastDirectory(baseDirectory);
            return baseDirectory;
        }

        /// <summary>
        /// Remove the last directory from the specified path.
        /// </summary>
        public static string RemoveLastDirectory(string path)
        {
            return path.Substring(0, path.Trim('\\').LastIndexOf('\\') + 1);
        }

        /// <summary>
        /// Find the specified file name in the base directory of the application or any subdirectories.
        /// </summary>
        public static string FindFile(string fileName)
        {
            // Look in the current directory first
            string baseDirectory = Environment.CurrentDirectory;
            string[] files = Directory.GetFiles(baseDirectory, fileName, SearchOption.AllDirectories);
            if (files.Length > 0)
                return files[0];

            // Get the project base directory if we're running in an output directory
            baseDirectory = GetBaseDirectory();
            files = Directory.GetFiles(baseDirectory, fileName, SearchOption.AllDirectories);
            if (files.Length > 0)
                return files[0];

            return null;
        }

        #endregion
    }
}

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 Common Development and Distribution License (CDDL)

Share

About the Author

KenBeckett
Software Developer (Senior)
United States United States
I've been writing software since the late 70's, currently focusing mainly on C#.NET. I also like to travel around the world, and I own a Chocolate Factory (sadly, none of my employees are oompa loompas).

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 2 Dec 2012
Article Copyright 2012 by KenBeckett
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid