Esempio n. 1
0
        /// <summary>
        /// Checks the path.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="create">if set to <c>true</c> [create].</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
        public static bool CheckPath(string path, bool create = false)
        {
            CDFMonitor.LogOutputHandler(string.Format("DEBUG:CheckPath: enter:{0}:{1}", path, create));

            try
            {
                if (string.IsNullOrEmpty(path))
                {
                    CDFMonitor.LogOutputHandler("CheckPath: empty path. returning false.");
                    return(false);
                }

                path = FileManager.GetFullPath(path);

                ResourceManagement rm = new ResourceManagement();
                ResourceManagement.ResourceType rt = rm.GetPathType(path);
                if (rm.CheckResourceCredentials(path) != ResourceManagement.CommandResults.Successful)
                {
                    // 131022 if multiple dirs in path do not exist above will fail.
                    if (!create | rm.DeterminePathObj(path) != ResourceManagement.DeterminePathObjType.Directory)
                    {
                        CDFMonitor.LogOutputHandler("CheckPath: checkresourcecredentials failed. returning false.");
                        return(false);
                    }
                }

                if (rt == ResourceManagement.ResourceType.Unc &&
                    rm.CheckUncPath(path, create))
                {
                    CDFMonitor.LogOutputHandler("CheckPath: able to access unc. returning true.");
                    return(true);
                }

                if (rt == ResourceManagement.ResourceType.Url)
                {
                    return(true);
                }

                ResourceManagement.DeterminePathObjType dt = rm.DeterminePathObj(path);

                if (dt == ResourceManagement.DeterminePathObjType.Directory)
                {
                    if (Directory.Exists(path))
                    {
                        return(true);
                    }

                    if (create)
                    {
                        Directory.CreateDirectory(path);
                        // reset creds in case they failed on non-existent dir above
                        if (rm.CheckResourceCredentials(path, true) != ResourceManagement.CommandResults.Successful)
                        {
                            return(false);
                        }
                        else
                        {
                            return(true);
                        }
                    }

                    CDFMonitor.LogOutputHandler("DEBUG:CheckPath: directory doesnt exist and not configured to create. returning false:" + path);
                    return(false);
                }

                if (dt == ResourceManagement.DeterminePathObjType.File)
                {
                    if (File.Exists(path) | Directory.Exists(Path.GetDirectoryName(path)))
                    {
                        return(true);
                    }

                    if (create)
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(path));
                        return(true);
                    }

                    CDFMonitor.LogOutputHandler("CheckPath: file doesnt exist and directory cannot be created. returning false.");
                    return(false);
                }

                CDFMonitor.LogOutputHandler(string.Format("CheckPath: unknown object:{0}", dt));
                return(false);
            }
            catch (Exception e)
            {
                CDFMonitor.LogOutputHandler("CheckPath: exception:" + e.ToString());
                return(false);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Gets the files.
        /// </summary>
        /// <param name="cleanPath">The path. Path can contain wildcard</param>
        /// <param name="wildcard">The wildcard. * default</param>
        /// <param name="subDir">The sub dir.</param>
        /// <returns>System.String[][].</returns>
        public static string[] GetFiles(string path, string wildcard = "*.*", SearchOption subDir = SearchOption.TopDirectoryOnly)
        {
            string             cleanPath = path.Trim('"');
            List <string>      retList   = new List <string>();
            ResourceManagement rm        = new ResourceManagement();

            ResourceManagement.DeterminePathObjType dt = new ResourceManagement.DeterminePathObjType();

            // sometimes wildcards in 'path' so override 'wildcard' if so
            if (Regex.IsMatch(cleanPath, @"[^\\]*$"))
            {
                // get path type before modifying dt = rm.DeterminePathObj(GetFullPath(path));

                string tempString = Regex.Match(cleanPath, @"[^\\]*$").Groups[0].Value;
                if (tempString.Contains("*") | tempString.Contains("?"))
                {
                    wildcard  = tempString;
                    cleanPath = cleanPath.Replace(string.Format(@"\{0}", tempString), "");
                }
            }

            dt = rm.DeterminePathObj(GetFullPath(cleanPath));

            if (string.IsNullOrEmpty(cleanPath) || string.IsNullOrEmpty(Path.GetDirectoryName(cleanPath)))
            {
                cleanPath = string.Format("{0}\\{1}", Environment.CurrentDirectory, cleanPath);
            }

            if (rm.GetPathType(cleanPath) == ResourceManagement.ResourceType.Unc &&
                rm.CheckUncPath(cleanPath, false))
            {
                if (!rm.ConnectUncPath(cleanPath))
                {
                    return(new string[0]);
                }
            }

            try
            {
                if (dt == ResourceManagement.DeterminePathObjType.Directory &&
                    Directory.Exists(cleanPath))
                {
                    try
                    {
                        retList.AddRange(Directory.GetFiles(cleanPath, wildcard, subDir));
                    }
                    catch (Exception e)
                    {
                        CDFMonitor.LogOutputHandler(string.Format("GetFiles:directory exception:{0}", e.ToString()));
                    }
                }
                else if (dt == ResourceManagement.DeterminePathObjType.File &&
                         File.Exists(cleanPath))
                {
                    retList.Add(cleanPath);
                }
                else if (dt == ResourceManagement.DeterminePathObjType.WildCard)
                {
                    try
                    {
                        retList.AddRange(Directory.GetFiles(Path.GetDirectoryName(cleanPath), Path.GetFileName(cleanPath), subDir));
                    }
                    catch (Exception e)
                    {
                        CDFMonitor.LogOutputHandler(string.Format("GetFiles:wildcard exception:{0}", e.ToString()));
                    }
                }
                else
                {
                    CDFMonitor.LogOutputHandler("GetFiles:Error:no files. returning.");
                    return(new string[0]);
                }

                CDFMonitor.LogOutputHandler(string.Format("GetFiles: returning {0} files.", retList.Count));
                return(retList.ToArray());
            }
            catch (Exception e)
            {
                CDFMonitor.LogOutputHandler(string.Format("GetFiles:main exception:{0}", e.ToString()));
                return(new string[0]);
            }
            finally
            {
                if (rm.GetPathType(cleanPath) == ResourceManagement.ResourceType.Unc)
                {
                    rm.DisconnectUncPath(cleanPath);
                }
            }
        }