|
2 | 2 | * COPYRIGHT: See COPYING in the top level directory |
3 | 3 | * PROJECT: FileHandler |
4 | 4 | * FILE: FileHandler/FileHandleCreate.cs |
5 | | - * PURPOSE: Does all types of File Operations, Create Files |
6 | | - * PROGRAMER: Peter Geinitz (Wayfarer) |
| 5 | + * PURPOSE: Handles all types of file creation operations |
| 6 | + * PROGRAMMER: Peter Geinitz (Wayfarer) |
7 | 7 | */ |
8 | 8 |
|
9 | | -// ReSharper disable UnusedMember.Global |
10 | | - |
11 | 9 | using System; |
12 | 10 | using System.Diagnostics; |
13 | 11 | using System.IO; |
14 | 12 |
|
15 | 13 | namespace FileHandler; |
16 | 14 |
|
17 | 15 | /// <summary> |
18 | | -/// The file handle create class. |
| 16 | +/// Handles file and folder creation. |
19 | 17 | /// </summary> |
20 | 18 | public static class FileHandleCreate |
21 | 19 | { |
22 | 20 | /// <summary> |
23 | | - /// Create a Folder |
| 21 | + /// Creates a folder at the specified path. |
24 | 22 | /// </summary> |
25 | | - /// <param name="path">path</param> |
26 | | - /// <exception cref="FileHandlerException">No Correct Path was provided</exception> |
| 23 | + /// <param name="path">Target folder path.</param> |
| 24 | + /// <exception cref="FileHandlerException">Thrown if the path is null or empty.</exception> |
27 | 25 | public static void CreateFolder(string path) |
28 | 26 | { |
29 | | - if (string.IsNullOrEmpty(path)) |
30 | | - { |
| 27 | + if (string.IsNullOrWhiteSpace(path)) |
31 | 28 | throw new FileHandlerException(FileHandlerResources.ErrorEmptyString); |
32 | | - } |
33 | 29 |
|
34 | 30 | _ = CreateDirectory(path); |
35 | 31 | } |
36 | 32 |
|
37 | 33 | /// <summary> |
38 | | - /// Creates a Folder in a specific path, based on the root folder |
| 34 | + /// Creates a folder with a specific name inside a parent path. |
39 | 35 | /// </summary> |
40 | | - /// <param name="path">Path</param> |
41 | | - /// <param name="name">Folder Name</param> |
42 | | - /// <returns>If path was generated </returns> |
43 | | - /// <exception cref="FileHandlerException">No Correct Path was provided</exception> |
| 36 | + /// <param name="path">Parent path.</param> |
| 37 | + /// <param name="name">Folder name.</param> |
| 38 | + /// <returns>True if the folder was created or already exists; false if creation failed.</returns> |
| 39 | + /// <exception cref="FileHandlerException">Thrown if the path or name is null or empty.</exception> |
44 | 40 | public static bool CreateFolder(string path, string name) |
45 | 41 | { |
46 | | - if (string.IsNullOrEmpty(path) || string.IsNullOrEmpty(name)) |
47 | | - { |
| 42 | + if (string.IsNullOrWhiteSpace(path) || string.IsNullOrWhiteSpace(name)) |
48 | 43 | throw new FileHandlerException(FileHandlerResources.ErrorEmptyString); |
49 | | - } |
50 | 44 |
|
51 | | - var root = Path.Combine(path, name); |
| 45 | + var fullPath = Path.Combine(path, name); |
52 | 46 |
|
53 | | - return CreateDirectory(root); |
| 47 | + return CreateDirectory(fullPath); |
54 | 48 | } |
55 | 49 |
|
56 | 50 | /// <summary> |
57 | | - /// Creates the directory. |
| 51 | + /// Creates the directory safely. |
58 | 52 | /// </summary> |
59 | | - /// <param name="path">The path.</param> |
60 | | - /// <returns>If path was generated </returns> |
| 53 | + /// <param name="path">Full folder path.</param> |
| 54 | + /// <returns>True if the folder was created or already exists; false if creation failed.</returns> |
61 | 55 | private static bool CreateDirectory(string path) |
62 | 56 | { |
63 | 57 | try |
64 | 58 | { |
65 | | - _ = Directory.CreateDirectory(path); |
| 59 | + // Directory.CreateDirectory is safe: it does nothing if the directory already exists. |
| 60 | + Directory.CreateDirectory(path); |
66 | 61 | return true; |
67 | 62 | } |
68 | | - catch (Exception ex) when (ex is UnauthorizedAccessException or IOException or PathTooLongException) |
| 63 | + catch (Exception ex) when (ex is UnauthorizedAccessException |
| 64 | + or IOException |
| 65 | + or PathTooLongException |
| 66 | + or NotSupportedException) |
69 | 67 | { |
70 | 68 | FileHandlerRegister.AddError(nameof(CreateFolder), path, ex); |
71 | 69 | Trace.WriteLine(ex); |
|
0 commit comments