Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- namespace com.wms.dirs{
- /*
- + https://gist.github.com/kyrathasoft/aca4a458ce58edb05f4476a8186cae33
- */
- public class ClassDirs{
- public static int CountDirectoriesRecursively(string targetDirectory)
- {
- int iDirs = 0; //if targetDirectory exists, returns total # of all subdirectories, even nested ones
- if(Directory.Exists(targetDirectory)){
- iDirs = Directory.GetDirectories(targetDirectory, "*", SearchOption.AllDirectories).Length;
- }
- return iDirs;
- }
- public static bool CopyTo(string targetDirectory, string destinationDirectory)
- {
- bool blnSuccess = false;
- try{
- if(Directory.Exists(targetDirectory)){
- foreach (string dirPath in Directory.GetDirectories(targetDirectory, "*",
- SearchOption.AllDirectories)){
- Directory.CreateDirectory(dirPath.Replace(targetDirectory, destinationDirectory));
- }
- //Copy all the files & Replaces any files with the same name
- foreach (string newPath in Directory.GetFiles(targetDirectory, "*.*",
- SearchOption.AllDirectories)){
- File.Copy(newPath, newPath.Replace(targetDirectory, destinationDirectory), true);
- blnSuccess = true;
- }
- }
- }catch(Exception exCopyTo){
- Console.WriteLine("Error in ioLibrary: " + exCopyTo.Message);
- }
- return blnSuccess;
- }
- public static int DirectoryDepth(string someValidPath)
- {
- int depth = -1;
- if(!IsValidPath(someValidPath)){
- return depth;
- }else{
- Console.Write(" Calculating directory depth... ");
- char[] splitter = new char[]{'\\'};
- string[] tokens = someValidPath.Split(splitter, StringSplitOptions.RemoveEmptyEntries);
- Console.Write("{0} deep\n", tokens.Length);
- for(int i=0; i < tokens.Length; i++){
- if(i % 2 == 0 && (i != 0)){
- Console.WriteLine("\tDepth {0}: {1}; ", (i + 1), tokens[i]);
- }else{
- Console.Write("\tDepth {0}: {1}; ", (i + 1), tokens[i]);
- }
- }
- Console.WriteLine("");
- }
- return depth;
- }
- public static int GetDirectoryFileCount(string path)
- {
- int fileCount = 0;
- if(Directory.Exists(path))
- {
- fileCount = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Length;
- }
- return fileCount;
- }
- public static List<string> GetListOfSubdirectoryPaths(string path){
- List<string> listing = new List<string>();
- DirectoryInfo di = new DirectoryInfo(path);
- DirectoryInfo[] directories = di.GetDirectories("*.*", SearchOption.AllDirectories);
- foreach(DirectoryInfo dinfo in directories){
- listing.Add(dinfo.FullName);
- }
- return listing;
- }
- public static string GetPathofCodeBase(){
- string result = string.Empty;
- string sPath = (System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
- sPath = sPath.Substring(8, sPath.Length - 8);
- sPath = Path.GetDirectoryName(sPath);
- result = sPath;
- return result;
- }
- //kept for backward compatibility: for instance, cproggen.cs needs it...
- public static string GetPathPortionOfFullPath(string fullpath)
- {
- string result = string.Empty;
- if(IsValidPath(fullpath)){
- string sFilename = Path.GetFileName(fullpath);
- int index = fullpath.IndexOf(sFilename);
- result = fullpath.Substring(0, index);
- }else{
- result = "A valid path wasn't passed into GetPathPortionOfFullPath()...";
- }
- return result;
- }
- public static string GetPathPortionOfFullPathToFile(string fullpathToFile)
- {
- string result = string.Empty;
- if(File.Exists(fullpathToFile)){
- string sFilename = Path.GetFileName(fullpathToFile);
- int index = fullpathToFile.IndexOf(sFilename);
- result = fullpathToFile.Substring(0, index);
- }else{
- //leave empty string assigned to result
- }
- return result;
- }
- public static bool IsValidPath(string path, bool allowRelativePaths = false)
- {
- bool isValid = true;
- try
- {
- string fullPath = Path.GetFullPath(path);
- if (allowRelativePaths)
- {
- isValid = Path.IsPathRooted(path);
- }
- else
- {
- string root = Path.GetPathRoot(path);
- isValid = string.IsNullOrEmpty(root.Trim(new char[] { '\\', '/' })) == false;
- }
- }
- catch(Exception ex)
- {
- isValid = false;
- Console.WriteLine("\n " + ex.Message + "\n\nMessage originated in dirs.dll method IsValidPath()...");
- }
- return isValid;
- }
- public static bool SpecifiedDirectoryContainsTheseFiles(string targetDirectory, List<string> _fileList)
- {
- bool result = false;
- int sought_count = _fileList.Count;
- if(Directory.Exists(targetDirectory)){
- string[] files = Directory.GetFiles(targetDirectory);
- int iCount = 0;
- foreach(string p in _fileList){
- for(int i=0; i < files.Length; i++){
- string sName = Path.GetFileName(files[i]);
- if(sName == p){ iCount++; }
- }
- }
- if(iCount == sought_count){ result = true; }
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement