コード例 #1
0
        /// <summary>
        /// Method to find if a path is in the environment PATH envvar
        /// </summary>
        /// <param name="path">The path to search for</param>
        /// <returns>returns True if found, False if not</returns>
        public bool IsPathInEnv(string path)
        {
            this.Log().Debug("Trying to search for a path in path envvar: " + path);
            string pathVar = "path";

            if (OsDetector.DetectOs() == OsDetector.OsSelection.Windows)
            {
                pathVar = "path";
            }
            else if (OsDetector.DetectOs() == OsDetector.OsSelection.Unix)
            {
                pathVar = "PATH";
            }

            if (EnvVars.ContainsKey(pathVar))
            {
                // try to get the path
                string pathValue = "";
                EnvVars.TryGetValue(pathVar, out pathValue);

                if (String.IsNullOrWhiteSpace(pathValue))
                {
                    // empty path? warn!
                    this.Log().Warn("Empty path environment variable. something could be wrong.");
                    return(false);
                }

                string lastChar = path.Substring(path.Length - 1, 1);
                if ((lastChar == "/") || (lastChar == @"\"))
                {
                    this.Log().Debug("Found a trailing slash. cutting it.");
                    path = path.Substring(0, path.Length - 1);
                }

                string[] arrayPaths = null;
                if (OsDetector.DetectOs() == OsDetector.OsSelection.Windows)
                {
                    arrayPaths = pathValue.Split(new char[] { ';' });
                }
                else if (OsDetector.DetectOs() == OsDetector.OsSelection.Unix)
                {
                    arrayPaths = pathValue.Split(new char[] { ':' });
                }

                foreach (string s in arrayPaths)
                {
                    if (string.Compare(s, path, true) == 0)
                    {
                        this.Log().Debug("Path found");
                        return(true);
                    }
                }
            }

            return(false);
        }
コード例 #2
0
        /// <summary>
        /// Method that itterates through the whole path environment and tries to find a specific file|path inside each folder
        /// </summary>
        /// <returns>returns the first path found that matches, or null if none</returns>
        /// <param name="path">the file|path to search for</param>
        public string FindInPath(string path)
        {
            this.Log().Debug("Trying to resolve a path|file in the path envvar: " + path);

            string pathVar = "path";

            if (OsDetector.DetectOs() == OsDetector.OsSelection.Windows)
            {
                pathVar = "path";
            }
            else if (OsDetector.DetectOs() == OsDetector.OsSelection.Unix)
            {
                pathVar = "PATH";
            }

            if (EnvVars.ContainsKey(pathVar))
            {
                // try to get the path
                string pathValue = "";
                EnvVars.TryGetValue(pathVar, out pathValue);

                if (String.IsNullOrWhiteSpace(pathValue))
                {
                    // empty path? warn!
                    this.Log().Warn("Empty path environment variable. something could be wrong.");
                    return(null);
                }

                string lastChar = path.Substring(path.Length - 1, 1);
                bool   isFolder = false;
                if ((lastChar == "/") || (lastChar == @"\"))
                {
                    // Folder mode!
                    this.Log().Debug("Found a trailing slash. Folder mode and removing the slash.");
                    isFolder = true;
                    path     = path.Substring(0, path.Length - 1);
                }

                string[] arrayPaths = null;
                if (OsDetector.DetectOs() == OsDetector.OsSelection.Windows)
                {
                    arrayPaths = pathValue.Split(new char[] { ';' });
                }
                else if (OsDetector.DetectOs() == OsDetector.OsSelection.Unix)
                {
                    arrayPaths = pathValue.Split(new char[] { ':' });
                }

                string toCheck = "";
                foreach (string s in arrayPaths)
                {
                    toCheck = Path.Combine(s, path);
                    if (isFolder)
                    {
                        if (Directory.Exists(toCheck))
                        {
                            this.Log().Debug(string.Format("Found matching path: {0}", toCheck));
                            return(toCheck);
                        }
                    }
                    else
                    {
                        if (File.Exists(toCheck))
                        {
                            this.Log().Debug(string.Format("Found matching file: {0}", toCheck));
                            return(toCheck);
                        }
                    }
                }
            }

            this.Log().Info("No path|file was resolved");
            return(null);
        }