Ejemplo n.º 1
0
        /// <summary>
        /// Deletes the folder.
        /// </summary>
        /// <param name="destinationPath">The destination path.</param>
        /// <param name="deleteOnlyIfEmpty">if set to <c>true</c> [delete only if empty].</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
        public static bool DeleteFolder(string destinationPath, bool deleteOnlyIfEmpty = false)
        {
            ResourceManagement rm = new ResourceManagement();

            try
            {
                if (rm.GetPathType(destinationPath) == ResourceManagement.ResourceType.Unc &&
                    rm.CheckUncPath(destinationPath, false))
                {
                    if (!rm.ConnectUncPath(Directory.GetParent(destinationPath).FullName))
                    {
                        return(false);
                    }
                }
                if (!Directory.Exists(destinationPath))
                {
                    CDFMonitor.LogOutputHandler("DeleteFolder:destination does not exist:" + destinationPath);
                    return(false);
                }

                if (deleteOnlyIfEmpty &&
                    (Directory.GetFiles(destinationPath).Length != 0 ||
                     Directory.GetDirectories(destinationPath).Length != 0))
                {
                    CDFMonitor.LogOutputHandler("DeleteFolder:destination is not empty:" + destinationPath);
                    return(false);
                }

                CDFMonitor.LogOutputHandler("DeleteFolder: deleting folder:" + destinationPath);
                Directory.Delete(destinationPath, true);
                return(true);
            }
            catch (Exception e)
            {
                CDFMonitor.LogOutputHandler("DeleteFolder:exception:" + e.ToString());
                return(false);
            }
            finally
            {
                if (rm.GetPathType(destinationPath) == ResourceManagement.ResourceType.Unc)
                {
                    rm.DisconnectUncPath(Directory.GetParent(destinationPath).FullName);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Renames the file.
        /// </summary>
        /// <param name="oldFileName">Old name of the file.</param>
        /// <param name="newFileName">New name of the file.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
        public static bool RenameFile(string oldFileName, string newFileName)
        {
            ResourceManagement rm = new ResourceManagement();

            if (rm.GetPathType(oldFileName) == ResourceManagement.ResourceType.Unc &&
                rm.CheckUncPath(oldFileName, false))
            {
                if (!rm.ConnectUncPath(oldFileName))
                {
                    return(false);
                }
            }
            try
            {
                CDFMonitor.LogOutputHandler(string.Format("RenameFile:file:{0}:{1}", oldFileName, newFileName));
                if (File.Exists(newFileName))
                {
                    File.Delete(newFileName);
                }

                File.Move(oldFileName, newFileName);
                return(true);
            }
            catch
            {
                CDFMonitor.LogOutputHandler(string.Format("RenameFile:exception renaming file:{0}", oldFileName));
                return(false);
            }
            finally
            {
                if (rm.GetPathType(oldFileName) == ResourceManagement.ResourceType.Unc)
                {
                    rm.DisconnectUncPath(oldFileName);
                }
            }
        }
Ejemplo n.º 3
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);
            }
        }
Ejemplo n.º 4
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);
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Deletes the file.
        /// </summary>
        /// <param name="destinationFiles">The destination files.</param>
        /// <param name="removeParentAfterDelete">if set to <c>true</c> [remove after delete].</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
        public static Results DeleteFiles(string[] destinationFiles, bool removeParentAfterDelete = true)
        {
            ResourceManagement rm      = new ResourceManagement();
            Results            results = Results.Success;

            try
            {
                foreach (string destinationFile in destinationFiles.Distinct())
                {
                    string path = Path.GetDirectoryName(destinationFile);

                    if (rm.GetPathType(path) == ResourceManagement.ResourceType.Unc &&
                        rm.CheckUncPath(path, false))
                    {
                        if (!rm.ConnectUncPath(path))
                        {
                            return(Results.Fail);
                        }
                    }

                    if (!File.Exists(destinationFile))
                    {
                        CDFMonitor.LogOutputHandler("DeleteFile:destination does not exist:" + destinationFile);
                        results = Results.SuccessWithErrors;
                        continue;
                    }

                    if (!FileInUse(destinationFile))
                    {
                        CDFMonitor.LogOutputHandler("DeleteFile: deleting file:" + destinationFile);
                        File.Delete(destinationFile);
                    }
                    else
                    {
                        CDFMonitor.LogOutputHandler("DeleteFile: file in use:" + destinationFile);
                        results = Results.SuccessWithErrors;
                        continue;
                    }

                    // try to remove parent but is just best effort
                    try
                    {
                        if (removeParentAfterDelete &&
                            Directory.GetFiles(path).Count() == 0 &&
                            Directory.GetDirectories(path).Count() == 0)
                        {
                            CDFMonitor.LogOutputHandler("DeleteFile: removing parent:" + path);
                            Directory.Delete(path);
                        }
                    }
                    catch { }

                    if (rm.GetPathType(destinationFile) == ResourceManagement.ResourceType.Unc)
                    {
                        rm.DisconnectUncPath(destinationFile);
                    }
                }

                return(results);
            }
            catch (Exception e)
            {
                CDFMonitor.LogOutputHandler("DeleteFile:exception:" + e.ToString());
                return(Results.Fail);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Copies the files.
        /// </summary>
        /// <param name="files">The files.</param>
        /// <param name="destinationPath">The destination path.</param>
        /// <param name="createDestination">if set to <c>true</c> [create destination].</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
        public static bool CopyFiles(string[] files, string destinationPath, bool createDestination = false)
        {
            string sourcePathRoot = string.Empty;
            string destPathRoot   = destinationPath.TrimEnd('\\');
            string destFile       = string.Empty;
            bool   retval         = true;

            try
            {
                CDFMonitor.LogOutputHandler(string.Format("DEBUG:CopyFiles:enter"));

                ResourceManagement rm = new ResourceManagement();

                if (rm.CheckResourceCredentials(destinationPath, false) != ResourceManagement.CommandResults.Successful)
                {
                    CDFMonitor.LogOutputHandler(string.Format("Error:CopyFiles:invalid credential return:{0}:{1}", rm, destinationPath));
                    return(false);
                }
                else if (!Directory.Exists(destinationPath))
                {
                    if (!createDestination)
                    {
                        CDFMonitor.LogOutputHandler("CopyFiles:destination does not exist and create = false:" + destinationPath);
                        return(false);
                    }

                    CDFMonitor.LogOutputHandler(string.Format("CopyFiles:creating directory:{0}", destinationPath));
                    Directory.CreateDirectory(destinationPath);
                }

                foreach (string file in files)
                {
                    string sourceFile = GetFullPath(file);

                    // populate sourcePathRoot if empty to avoid errors below
                    if (string.IsNullOrEmpty(sourcePathRoot))
                    {
                        sourcePathRoot = Path.GetDirectoryName(sourceFile);
                    }

                    CDFMonitor.LogOutputHandler(string.Format("CopyFiles:copying file:{0} : {1}", sourceFile, destinationPath));
                    if (rm.GetPathType(sourceFile) == ResourceManagement.ResourceType.Unc &&
                        rm.CheckResourceCredentials(sourceFile) == ResourceManagement.CommandResults.Successful)
                    {
                        if (!rm.ConnectUncPath(sourceFile))
                        {
                            retval = false;
                            continue;
                        }
                    }

                    // setup dest path and directory
                    if (String.Compare(sourcePathRoot, Path.GetDirectoryName(sourceFile)) != 0)
                    {
                        string newSourcePath = Path.GetDirectoryName(sourceFile);
                        if (newSourcePath.Contains(sourcePathRoot))
                        {
                            // then its a subdirectory. add dir to dest
                            destFile = sourceFile.Replace(sourcePathRoot, destPathRoot);
                            string newdestPath = Path.GetDirectoryName(destFile);

                            if (!Directory.Exists(newdestPath))
                            {
                                Directory.CreateDirectory(newdestPath);
                            }
                        }
                        else
                        {
                            // new root
                            sourcePathRoot = newSourcePath;
                            destFile       = sourceFile.Replace(sourcePathRoot, destPathRoot);
                        }
                    }
                    else
                    {
                        // no change in path
                        destFile = sourceFile.Replace(sourcePathRoot, destPathRoot);
                    }

                    if (!File.Exists(sourceFile))
                    {
                        CDFMonitor.LogOutputHandler(string.Format("Warning:CopyFiles:source file does not exist:{0}", sourceFile));
                        retval = false;
                        continue;
                    }

                    if (File.Exists(destFile))
                    {
                        File.Delete(destFile);
                    }

                    CDFMonitor.LogOutputHandler(string.Format("CopyFiles:copying file source:{0} dest:{1}", sourceFile, destFile));
                    File.Copy(sourceFile, destFile, true);

                    if (rm.GetPathType(sourceFile) == ResourceManagement.ResourceType.Unc)
                    {
                        rm.DisconnectUncPath(destinationPath);
                    }
                }

                if (rm.GetPathType(destinationPath) == ResourceManagement.ResourceType.Unc)
                {
                    rm.DisconnectUncPath(destinationPath);
                }

                return(retval);
            }
            catch (Exception e)
            {
                CDFMonitor.LogOutputHandler("CopyFiles:exception:" + e.ToString());
                return(false);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// enumerates base logfile name and logfilename with index ex. cdfmonitor.log and
        /// cdfmonitor.0000.log
        /// </summary>
        /// <param name="logFile">base log file name</param>
        /// <param name="pattern">The pattern.</param>
        /// <returns>list of files found in order of lastwrite</returns>
        private List <string> EnumerateLogFiles(string logFile, string pattern, bool includeBase)
        {
            logFile = FileManager.GetFullPath(logFile);
            string logFilePath = string.IsNullOrEmpty(Path.GetDirectoryName(logFile)) ||
                                 !Directory.Exists(Path.GetDirectoryName(logFile))
                ? AppDomain.CurrentDomain.BaseDirectory
                : Path.GetDirectoryName(logFile);

            if (string.IsNullOrEmpty(logFile))
            {
                return(new List <string>());
            }

            ResourceManagement rm = new ResourceManagement();

            if (rm.CheckResourceCredentials(logFile) != ResourceManagement.CommandResults.Successful)
            {
                return(new List <string>());
            }

            if (rm.GetPathType(Path.GetDirectoryName(logFile)) == ResourceManagement.ResourceType.Unc)
            {
                rm.ConnectUncPath(Path.GetDirectoryName(logFile));
            }

            List <FileInfo> tempList = new List <FileInfo>();
            DirectoryInfo   dI       = new DirectoryInfo(logFilePath);

            string logFileBaseName = Regex.Replace(Path.GetFileName(logFile), @"\.([0-9]{1,4})\..{1,3}.{0,3}$", Path.GetExtension(logFile));

            pattern = string.Format(@"(?<beginning>{0})\.(?<index>{1})(?<end>{2})$", Path.GetFileNameWithoutExtension(logFileBaseName), pattern, Path.GetExtension(logFile));

            // Add all files with index in name
            tempList.AddRange(dI.GetFiles().Where(f => Regex.IsMatch(f.Name, pattern, RegexOptions.IgnoreCase)));

            // Add original name if it exists and different
            if (includeBase & !tempList.Any(i => i.FullName == logFile) & FileManager.FileExists(logFile))
            {
                // Add original name
                tempList.Add(new FileInfo(logFile));
            }
            else if (includeBase & !tempList.Any(i => i.FullName == string.Format("{0}\\{1}", logFilePath, logFileBaseName)) &
                     FileManager.FileExists(string.Format("{0}\\{1}", logFilePath, logFileBaseName)))
            {
                // Add base name
                tempList.AddRange(dI.GetFiles(logFileBaseName, SearchOption.TopDirectoryOnly));
            }

            //tempList.Sort((x, y) => DateTime.Compare(x.CreationTime, y.CreationTime));
            tempList.Sort((x, y) => DateTime.Compare(x.LastWriteTime, y.LastWriteTime));

            _cdfMonitor.LogOutput("DEBUG:EnumerateLogFiles file list:");
            foreach (FileInfo path in tempList)
            {
                _cdfMonitor.LogOutput("DEBUG:EnumerateLogFiles file:" + path.Name);
            }

            if (rm.GetPathType(logFile) == ResourceManagement.ResourceType.Unc)
            {
                rm.DisconnectUncPath(logFile);
            }

            return(tempList.Select(x => x.FullName).ToList());
        }