Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4.  
  5. namespace com.wms.dirs{
  6.  
  7. public class ClassDirs{
  8.  
  9. public static int CountDirectoriesRecursively(string targetDirectory)
  10. {
  11. int iDirs = 0; //if targetDirectory exists, returns total # of all subdirectories, even nested ones
  12.  
  13. if(Directory.Exists(targetDirectory)){
  14. iDirs = Directory.GetDirectories(targetDirectory, "*", SearchOption.AllDirectories).Length;
  15. }
  16.  
  17. return iDirs;
  18. }
  19.  
  20. public static bool CopyTo(string targetDirectory, string destinationDirectory)
  21. {
  22. bool blnSuccess = false;
  23.  
  24. try{
  25. if(Directory.Exists(targetDirectory)){
  26. foreach (string dirPath in Directory.GetDirectories(targetDirectory, "*",
  27. SearchOption.AllDirectories)){
  28. Directory.CreateDirectory(dirPath.Replace(targetDirectory, destinationDirectory));
  29. }
  30. //Copy all the files & Replaces any files with the same name
  31. foreach (string newPath in Directory.GetFiles(targetDirectory, "*.*",
  32. SearchOption.AllDirectories)){
  33. File.Copy(newPath, newPath.Replace(targetDirectory, destinationDirectory), true);
  34. blnSuccess = true;
  35. }
  36. }
  37. }catch(Exception exCopyTo){
  38. Console.WriteLine("Error in ioLibrary: " + exCopyTo.Message);
  39. }
  40.  
  41. return blnSuccess;
  42.  
  43. }
  44.  
  45. public static int DirectoryDepth(string someValidPath)
  46. {
  47. int depth = -1;
  48.  
  49. if(!IsValidPath(someValidPath)){
  50. return depth;
  51. }else{
  52. Console.Write(" Calculating directory depth... ");
  53. char[] splitter = new char[]{'\\'};
  54. string[] tokens = someValidPath.Split(splitter, StringSplitOptions.RemoveEmptyEntries);
  55. Console.Write("{0} deep\n", tokens.Length);
  56.  
  57. for(int i=0; i < tokens.Length; i++){
  58. if(i % 2 == 0 && (i != 0)){
  59. Console.WriteLine("\tDepth {0}: {1}; ", (i + 1), tokens[i]);
  60. }else{
  61. Console.Write("\tDepth {0}: {1}; ", (i + 1), tokens[i]);
  62. }
  63. }
  64. Console.WriteLine("");
  65.  
  66. }
  67.  
  68. return depth;
  69. }
  70.  
  71. public static int GetDirectoryFileCount(string path)
  72. {
  73. int fileCount = 0;
  74.  
  75. if(Directory.Exists(path))
  76. {
  77. fileCount = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Length;
  78. }
  79.  
  80. return fileCount;
  81. }
  82.  
  83. public static List<string> GetListOfSubdirectoryPaths(string path){
  84. List<string> listing = new List<string>();
  85. DirectoryInfo di = new DirectoryInfo(path);
  86. DirectoryInfo[] directories = di.GetDirectories("*.*", SearchOption.AllDirectories);
  87. foreach(DirectoryInfo dinfo in directories){
  88. listing.Add(dinfo.FullName);
  89. }
  90. return listing;
  91. }
  92.  
  93. public static string GetPathofCodeBase(){
  94. string result = string.Empty;
  95. string sPath = (System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
  96. sPath = sPath.Substring(8, sPath.Length - 8);
  97. sPath = Path.GetDirectoryName(sPath);
  98. result = sPath;
  99. return result;
  100. }
  101.  
  102. //kept for backward compatibility: for instance, cproggen.cs needs it...
  103. public static string GetPathPortionOfFullPath(string fullpath)
  104. {
  105. string result = string.Empty;
  106. if(IsValidPath(fullpath)){
  107. string sFilename = Path.GetFileName(fullpath);
  108. int index = fullpath.IndexOf(sFilename);
  109. result = fullpath.Substring(0, index);
  110. }else{
  111. result = "A valid path wasn't passed into GetPathPortionOfFullPath()...";
  112. }
  113. return result;
  114. }
  115.  
  116. public static string GetPathPortionOfFullPathToFile(string fullpathToFile)
  117. {
  118. string result = string.Empty;
  119. if(File.Exists(fullpathToFile)){
  120. string sFilename = Path.GetFileName(fullpathToFile);
  121. int index = fullpathToFile.IndexOf(sFilename);
  122. result = fullpathToFile.Substring(0, index);
  123. }else{
  124. //leave empty string assigned to result
  125. }
  126. return result;
  127. }
  128.  
  129. public static bool IsValidPath(string path, bool allowRelativePaths = false)
  130. {
  131. bool isValid = true;
  132.  
  133. try
  134. {
  135. string fullPath = Path.GetFullPath(path);
  136.  
  137. if (allowRelativePaths)
  138. {
  139. isValid = Path.IsPathRooted(path);
  140. }
  141. else
  142. {
  143. string root = Path.GetPathRoot(path);
  144. isValid = string.IsNullOrEmpty(root.Trim(new char[] { '\\', '/' })) == false;
  145. }
  146. }
  147. catch(Exception ex)
  148. {
  149. isValid = false;
  150. Console.WriteLine("\n " + ex.Message + "\n\nMessage originated in dirs.dll method IsValidPath()...");
  151. }
  152.  
  153. return isValid;
  154. }
  155.  
  156. public static bool SpecifiedDirectoryContainsTheseFiles(string targetDirectory, List<string> _fileList)
  157. {
  158. bool result = false;
  159. int sought_count = _fileList.Count;
  160. if(Directory.Exists(targetDirectory)){
  161. string[] files = Directory.GetFiles(targetDirectory);
  162. int iCount = 0;
  163. foreach(string p in _fileList){
  164. for(int i=0; i < files.Length; i++){
  165. string sName = Path.GetFileName(files[i]);
  166. if(sName == p){ iCount++; }
  167. }
  168. }
  169. if(iCount == sought_count){ result = true; }
  170. }
  171.  
  172. return result;
  173. }
  174.  
  175. }
  176.  
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement