File tree Expand file tree Collapse file tree 1 file changed +18
-17
lines changed
snippets/csharp/System.IO/Directory/CreateDirectory Expand file tree Collapse file tree 1 file changed +18
-17
lines changed Original file line number Diff line number Diff line change @@ -8,29 +8,30 @@ public static void Main()
88 {
99 // Specify the directory you want to manipulate.
1010 string path = @"c:\MyDir" ;
11-
11+
12+ // Determine whether the directory exists.
13+ if ( Directory . Exists ( path ) )
14+ {
15+ Console . WriteLine ( "That path exists already." ) ;
16+ return ;
17+ }
18+
19+ DirectoryInfo di ;
1220 try
1321 {
14- // Determine whether the directory exists.
15- if ( Directory . Exists ( path ) )
16- {
17- Console . WriteLine ( "That path exists already." ) ;
18- return ;
19- }
20-
2122 // Try to create the directory.
22- DirectoryInfo di = Directory . CreateDirectory ( path ) ;
23+ di = Directory . CreateDirectory ( path ) ;
2324 Console . WriteLine ( "The directory was created successfully at {0}." , Directory . GetCreationTime ( path ) ) ;
24-
25- // Delete the directory.
26- di . Delete ( ) ;
27- Console . WriteLine ( "The directory was deleted successfully." ) ;
2825 }
29- catch ( Exception e )
26+ catch ( UnauthorizedAccessException e )
3027 {
31- Console . WriteLine ( "The process failed: {0}" , e . ToString ( ) ) ;
28+ Console . WriteLine ( "The caller does not have the required permission to create `{0}`" , path ) ;
29+ return ;
3230 }
33- finally { }
31+
32+ // Delete the directory.
33+ di . Delete ( ) ;
34+ Console . WriteLine ( "The directory was deleted successfully." ) ;
3435 }
3536}
36- // </Snippet1>
37+ // </Snippet1>
You can’t perform that action at this time.
0 commit comments