Skip to content

Commit 938aaa7

Browse files
authored
Code Quality: Migrated CopyFileFromApp and MoveFileFromApp to CsWin32 (#15131)
1 parent 5fcd216 commit 938aaa7

File tree

7 files changed

+23
-28
lines changed

7 files changed

+23
-28
lines changed

src/Files.App/Helpers/Interop/NativeFileOperationsHelper.cs

-17
Original file line numberDiff line numberDiff line change
@@ -142,23 +142,6 @@ public static extern bool CreateDirectoryFromApp(
142142
IntPtr SecurityAttributes
143143
);
144144

145-
[DllImport("api-ms-win-core-file-fromapp-l1-1-0.dll", CharSet = CharSet.Auto,
146-
CallingConvention = CallingConvention.StdCall,
147-
SetLastError = true)]
148-
public static extern bool MoveFileFromApp(
149-
string lpExistingFileName,
150-
string lpNewFileName
151-
);
152-
153-
[DllImport("api-ms-win-core-file-fromapp-l1-1-0.dll", CharSet = CharSet.Auto,
154-
CallingConvention = CallingConvention.StdCall,
155-
SetLastError = true)]
156-
public static extern bool CopyFileFromApp(
157-
string lpExistingFileName,
158-
string lpNewFileName,
159-
bool bFailIfExists
160-
);
161-
162145
[DllImport("api-ms-win-core-file-fromapp-l1-1-0.dll", CharSet = CharSet.Auto,
163146
CallingConvention = CallingConvention.StdCall,
164147
SetLastError = true)]

src/Files.App/NativeMethods.txt

+2
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,5 @@ AttachThreadInput
3939
SetWindowPos
4040
SetFocus
4141
SetActiveWindow
42+
CopyFileFromApp
43+
MoveFileFromApp

src/Files.App/Utils/Storage/Helpers/FilesystemResult.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) 2024 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4+
using Windows.Win32.Foundation;
5+
46
namespace Files.App.Utils.Storage
57
{
68
public class FilesystemResult
@@ -12,8 +14,12 @@ public class FilesystemResult
1214
public static implicit operator FileSystemStatusCode(FilesystemResult res) => res.ErrorCode;
1315
public static implicit operator FilesystemResult(FileSystemStatusCode res) => new(res);
1416

15-
public static implicit operator bool(FilesystemResult res) => res is not null && res.ErrorCode is FileSystemStatusCode.Success;
17+
public static implicit operator bool(FilesystemResult res) => res?.ErrorCode is FileSystemStatusCode.Success;
1618
public static explicit operator FilesystemResult(bool res) => new(res ? FileSystemStatusCode.Success : FileSystemStatusCode.Generic);
19+
20+
21+
public static implicit operator BOOL(FilesystemResult res) => res?.ErrorCode is FileSystemStatusCode.Success;
22+
public static explicit operator FilesystemResult(BOOL res) => new(res ? FileSystemStatusCode.Success : FileSystemStatusCode.Generic);
1723
}
1824

1925
public sealed class FilesystemResult<T> : FilesystemResult

src/Files.App/Utils/Storage/Operations/FilesystemOperations.cs

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Text;
88
using Windows.Foundation.Metadata;
99
using Windows.Storage;
10+
using Windows.Win32;
1011

1112
namespace Files.App.Utils.Storage
1213
{
@@ -210,7 +211,7 @@ await DialogDisplayHelper.ShowDialogAsync(
210211
}
211212
else if (source.ItemType == FilesystemItemType.File)
212213
{
213-
var fsResult = (FilesystemResult)await Task.Run(() => NativeFileOperationsHelper.CopyFileFromApp(source.Path, destination, true));
214+
var fsResult = (FilesystemResult)await Task.Run(() => PInvoke.CopyFileFromApp(source.Path, destination, true));
214215

215216
if (!fsResult)
216217
{
@@ -363,7 +364,7 @@ await DialogDisplayHelper.ShowDialogAsync(
363364
}
364365
else
365366
{
366-
var fsResult = (FilesystemResult)await Task.Run(() => NativeFileOperationsHelper.MoveFileFromApp(source.Path, destination));
367+
var fsResult = (FilesystemResult)await Task.Run(() => PInvoke.MoveFileFromApp(source.Path, destination));
367368

368369
if (!fsResult)
369370
{
@@ -423,7 +424,7 @@ await DialogDisplayHelper.ShowDialogAsync(
423424
}
424425
else if (source.ItemType == FilesystemItemType.File)
425426
{
426-
var fsResult = (FilesystemResult)await Task.Run(() => NativeFileOperationsHelper.MoveFileFromApp(source.Path, destination));
427+
var fsResult = (FilesystemResult)await Task.Run(() => PInvoke.MoveFileFromApp(source.Path, destination));
427428

428429
if (!fsResult)
429430
{
@@ -620,7 +621,7 @@ public async Task<IStorageHistory> RenameAsync(
620621
{
621622
// Try again with MoveFileFromApp
622623
var destination = Path.Combine(Path.GetDirectoryName(source.Path), newName);
623-
if (NativeFileOperationsHelper.MoveFileFromApp(source.Path, destination))
624+
if (PInvoke.MoveFileFromApp(source.Path, destination))
624625
{
625626
fsProgress.ReportStatus(FileSystemStatusCode.Success);
626627

@@ -729,7 +730,7 @@ public async Task<IStorageHistory> RestoreFromTrashAsync(IStorageItemWithPath so
729730

730731
FilesystemResult fsResult = FileSystemStatusCode.InProgress;
731732

732-
fsResult = (FilesystemResult)await Task.Run(() => NativeFileOperationsHelper.MoveFileFromApp(source.Path, destination));
733+
fsResult = (FilesystemResult)await Task.Run(() => PInvoke.MoveFileFromApp(source.Path, destination));
733734

734735
if (!fsResult)
735736
{

src/Files.App/Utils/Storage/StorageItems/NativeStorageFile.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Windows.Storage;
1010
using Windows.Storage.FileProperties;
1111
using Windows.Storage.Streams;
12+
using Windows.Win32;
1213
using IO = System.IO;
1314

1415
namespace Files.App.Utils.Storage
@@ -74,7 +75,7 @@ public override IAsyncOperation<BaseStorageFile> CopyAsync(IStorageFolder destin
7475
var destFile = new NativeStorageFile(destination, desiredNewName, DateTime.Now);
7576
if (!IsAlternateStream)
7677
{
77-
if (!await Task.Run(() => NativeFileOperationsHelper.CopyFileFromApp(Path, destination, option != NameCollisionOption.ReplaceExisting)))
78+
if (!await Task.Run(() => PInvoke.CopyFileFromApp(Path, destination, option != NameCollisionOption.ReplaceExisting)))
7879
{
7980
throw new Win32Exception(Marshal.GetLastWin32Error());
8081
}
@@ -185,7 +186,7 @@ public override IAsyncAction MoveAsync(IStorageFolder destinationFolder, string
185186
var destination = IO.Path.Combine(destinationFolder.Path, desiredNewName);
186187
if (!IsAlternateStream)
187188
{
188-
if (!await Task.Run(() => NativeFileOperationsHelper.MoveFileFromApp(Path, destination)))
189+
if (!await Task.Run(() => PInvoke.MoveFileFromApp(Path, destination)))
189190
{
190191
throw new Win32Exception(Marshal.GetLastWin32Error());
191192
}
@@ -237,7 +238,7 @@ public override IAsyncAction RenameAsync(string desiredName, NameCollisionOption
237238
var destFile = new NativeStorageFile(destination, desiredName, DateTime.Now);
238239
if (!IsAlternateStream)
239240
{
240-
if (!await Task.Run(() => NativeFileOperationsHelper.MoveFileFromApp(Path, destination)))
241+
if (!await Task.Run(() => PInvoke.MoveFileFromApp(Path, destination)))
241242
{
242243
throw new Win32Exception(Marshal.GetLastWin32Error());
243244
}

src/Files.App/Utils/Storage/StorageItems/ZipStorageFile.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Windows.Storage;
1010
using Windows.Storage.FileProperties;
1111
using Windows.Storage.Streams;
12+
using Windows.Win32;
1213
using IO = System.IO;
1314

1415
namespace Files.App.Utils.Storage
@@ -310,7 +311,7 @@ public override IAsyncAction RenameAsync(string desiredName, NameCollisionOption
310311
else
311312
{
312313
var fileName = IO.Path.Combine(IO.Path.GetDirectoryName(Path), desiredName);
313-
NativeFileOperationsHelper.MoveFileFromApp(Path, fileName);
314+
PInvoke.MoveFileFromApp(Path, fileName);
314315
}
315316
}
316317
else

src/Files.App/Utils/Storage/StorageItems/ZipStorageFolder.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Windows.Storage;
1212
using Windows.Storage.FileProperties;
1313
using Windows.Storage.Search;
14+
using Windows.Win32;
1415
using IO = System.IO;
1516

1617
namespace Files.App.Utils.Storage
@@ -350,7 +351,7 @@ public override IAsyncAction RenameAsync(string desiredName, NameCollisionOption
350351
else
351352
{
352353
var fileName = IO.Path.Combine(IO.Path.GetDirectoryName(Path), desiredName);
353-
NativeFileOperationsHelper.MoveFileFromApp(Path, fileName);
354+
PInvoke.MoveFileFromApp(Path, fileName);
354355
}
355356
}
356357
else

0 commit comments

Comments
 (0)