1+ using ComputerUtils . Logging ;
2+ using System ;
3+ using System . IO ;
4+
5+ namespace ComputerUtils . FileManaging
6+ {
7+ public class FileManager
8+ {
9+ public static long GetDirSize ( string dir )
10+ {
11+ return GetDirSize ( new DirectoryInfo ( dir ) ) ;
12+ }
13+
14+ public static long GetDirSize ( DirectoryInfo d )
15+ {
16+ long size = 0 ;
17+ FileInfo [ ] fis = d . GetFiles ( ) ;
18+ foreach ( FileInfo fi in fis )
19+ {
20+ size += fi . Length ;
21+ }
22+ DirectoryInfo [ ] dis = d . GetDirectories ( ) ;
23+ foreach ( DirectoryInfo di in dis )
24+ {
25+ size += GetDirSize ( di ) ;
26+ }
27+ return size ;
28+ }
29+
30+ public static string GetParentDirIfExisting ( string dir )
31+ {
32+ try
33+ {
34+ DirectoryInfo i = Directory . GetParent ( dir ) ;
35+ if ( i == null ) return dir ;
36+ return i . FullName ;
37+ }
38+ catch
39+ {
40+ return dir ;
41+ }
42+ }
43+ public static void DirectoryCopy ( string sourceDirName , string destDirName , bool copySubDirs , bool output = true )
44+ {
45+ // Get the subdirectories for the specified directory.
46+ try
47+ {
48+ if ( Directory . Exists ( destDirName ) ) Directory . Delete ( destDirName , true ) ;
49+ }
50+ catch { Console . ForegroundColor = ConsoleColor . Red ; Console . WriteLine ( "Couldn't delete " + destDirName ) ; Console . ForegroundColor = ConsoleColor . White ; }
51+
52+ DirectoryInfo dir = new DirectoryInfo ( sourceDirName ) ;
53+
54+ if ( ! dir . Exists )
55+ {
56+ throw new DirectoryNotFoundException (
57+ "Source directory does not exist or could not be found: "
58+ + sourceDirName ) ;
59+ }
60+
61+ DirectoryInfo [ ] dirs = dir . GetDirectories ( ) ;
62+
63+ // If the destination directory doesn't exist, create it.
64+ Directory . CreateDirectory ( destDirName ) ;
65+
66+ // Get the files in the directory and copy them to the new location.
67+ FileInfo [ ] files = dir . GetFiles ( ) ;
68+ foreach ( FileInfo file in files )
69+ {
70+ try
71+ {
72+ if ( output ) Console . WriteLine ( "Copying " + file . Name ) ;
73+ Logger . Log ( "Copying " + file . Name ) ;
74+ string tempPath = System . IO . Path . Combine ( destDirName , file . Name ) ;
75+ file . CopyTo ( tempPath , true ) ;
76+ }
77+ catch ( Exception e ) { Console . ForegroundColor = ConsoleColor . Red ; Console . WriteLine ( "ERROR copying " + file . Name ) ; Console . ForegroundColor = ConsoleColor . White ; Logger . Log ( "Error copying " + file . Name + ": " + e . ToString ( ) , LoggingType . Error ) ; }
78+ }
79+
80+ // If copying subdirectories, copy them and their contents to new location.
81+ if ( copySubDirs )
82+ {
83+ foreach ( DirectoryInfo subdir in dirs )
84+ {
85+ string tempPath = System . IO . Path . Combine ( destDirName , subdir . Name ) ;
86+ DirectoryCopy ( subdir . FullName , tempPath , copySubDirs ) ;
87+ }
88+ }
89+ }
90+
91+ public static DirectoryInfo CreateDirectoryIfNotExisting ( string path )
92+ {
93+ if ( ! Directory . Exists ( path ) )
94+ {
95+ Logger . Log ( "Creating " + path ) ;
96+ Directory . CreateDirectory ( path ) ;
97+ }
98+ return new DirectoryInfo ( path ) ;
99+ }
100+
101+ public static DirectoryInfo RecreateDirectoryIfExisting ( string path )
102+ {
103+
104+ if ( Directory . Exists ( path ) )
105+ {
106+ Logger . Log ( "Deleting " + path ) ;
107+ SetAttributesNormal ( new DirectoryInfo ( path ) ) ;
108+ Directory . Delete ( path , true ) ;
109+ }
110+ Logger . Log ( "Creating " + path ) ;
111+ Directory . CreateDirectory ( path ) ;
112+ return new DirectoryInfo ( path ) ;
113+ }
114+
115+ public static void SetAttributesNormal ( DirectoryInfo dir )
116+ {
117+ foreach ( DirectoryInfo subDir in dir . GetDirectories ( ) ) SetAttributesNormal ( subDir ) ;
118+ foreach ( FileInfo file in dir . GetFiles ( ) ) file . Attributes = FileAttributes . Normal ;
119+ }
120+ }
121+ }
0 commit comments