diff options
Diffstat (limited to 'src/WixToolset.Core.Native/FileSystem.cs')
| -rw-r--r-- | src/WixToolset.Core.Native/FileSystem.cs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/WixToolset.Core.Native/FileSystem.cs b/src/WixToolset.Core.Native/FileSystem.cs new file mode 100644 index 00000000..b9691d44 --- /dev/null +++ b/src/WixToolset.Core.Native/FileSystem.cs | |||
| @@ -0,0 +1,61 @@ | |||
| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. | ||
| 2 | |||
| 3 | namespace WixToolset.Core.Native | ||
| 4 | { | ||
| 5 | using System; | ||
| 6 | using System.IO; | ||
| 7 | using System.Runtime.InteropServices; | ||
| 8 | |||
| 9 | /// <summary> | ||
| 10 | /// File system helpers. | ||
| 11 | /// </summary> | ||
| 12 | public static class FileSystem | ||
| 13 | { | ||
| 14 | /// <summary> | ||
| 15 | /// Copies a file. | ||
| 16 | /// </summary> | ||
| 17 | /// <param name="source">The file to copy.</param> | ||
| 18 | /// <param name="destination">The destination file.</param> | ||
| 19 | /// <param name="allowHardlink">Allow hardlinks.</param> | ||
| 20 | public static void CopyFile(string source, string destination, bool allowHardlink) | ||
| 21 | { | ||
| 22 | if (File.Exists(destination)) | ||
| 23 | { | ||
| 24 | File.Delete(destination); | ||
| 25 | } | ||
| 26 | |||
| 27 | if (!allowHardlink || !CreateHardLink(destination, source, IntPtr.Zero)) | ||
| 28 | { | ||
| 29 | #if DEBUG | ||
| 30 | var er = Marshal.GetLastWin32Error(); | ||
| 31 | #endif | ||
| 32 | |||
| 33 | File.Copy(source, destination, overwrite: true); | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | /// <summary> | ||
| 38 | /// Moves a file. | ||
| 39 | /// </summary> | ||
| 40 | /// <param name="source">The file to move.</param> | ||
| 41 | /// <param name="destination">The destination file.</param> | ||
| 42 | public static void MoveFile(string source, string destination) | ||
| 43 | { | ||
| 44 | if (File.Exists(destination)) | ||
| 45 | { | ||
| 46 | File.Delete(destination); | ||
| 47 | } | ||
| 48 | |||
| 49 | var directory = Path.GetDirectoryName(destination); | ||
| 50 | if (!String.IsNullOrEmpty(directory)) | ||
| 51 | { | ||
| 52 | Directory.CreateDirectory(directory); | ||
| 53 | } | ||
| 54 | |||
| 55 | File.Move(source, destination); | ||
| 56 | } | ||
| 57 | |||
| 58 | [DllImport("Kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] | ||
| 59 | private static extern bool CreateHardLink(string lpFileName, string lpExistingFileName, IntPtr lpSecurityAttributes); | ||
| 60 | } | ||
| 61 | } | ||
