-
Notifications
You must be signed in to change notification settings - Fork 190
/
ShellFileOperationsTests.cs
112 lines (104 loc) · 3.54 KB
/
ShellFileOperationsTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using NUnit.Framework;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Vanara.Windows.Shell;
using static Vanara.PInvoke.Ole32;
using static Vanara.PInvoke.Shell32;
namespace UnitTests
{
[TestFixture]
public class ShellFileOperationsTests
{
[Test]
public void CopyItemTest()
{
ShellFileOperations.Copy(new ShellItem(@"C:\Users\dahall\Downloads\lubuntu-16.04.2-desktop-amd64.iso"), ShellFolder.Desktop);
Assert.That(File.Exists(@"C:\Users\dahall\Desktop\lubuntu-16.04.2-desktop-amd64.iso"), Is.True);
File.Delete(@"C:\Users\dahall\Desktop\lubuntu-16.04.2-desktop-amd64.iso");
}
[Test]
public void CopyItemsTest()
{
var l = Directory.EnumerateFiles(@"C:\Users\dahall\Downloads", "h*.zip").Select(s => new ShellItem(s)).ToList();
ShellFileOperations.Copy(l, ShellFolder.Desktop);
foreach (var i in l)
{
var fn = Path.Combine(@"C:\Users\dahall\Desktop", i.Name);
Assert.That(File.Exists(fn), Is.True);
File.Delete(fn);
}
}
[Test]
public void MultOpsTest()
{
using (var op = new ShellFileOperations())
{
op.Options |= ShellFileOperations.OperationFlags.NoMinimizeBox;
var shi = new ShellItem(@"C:\Users\dahall\Downloads\lubuntu-16.04.2-desktop-amd64.iso");
op.PostCopyItem += HandleEvent;
op.QueueCopyOperation(shi, ShellFolder.Desktop);
shi = new ShellItem(@"C:\Users\dahall\Desktop\lubuntu-16.04.2-desktop-amd64.iso");
op.QueueMoveOperation(shi, new ShellFolder(KNOWNFOLDERID.FOLDERID_Documents));
op.PostMoveItem += HandleEvent;
shi = new ShellItem(@"C:\Users\dahall\Documents\lubuntu-16.04.2-desktop-amd64.iso");
op.QueueRenameOperation(shi, "MuchLongerNameForTheFile.iso");
op.PostRenameItem += HandleEvent;
shi = new ShellItem(@"C:\Users\dahall\Documents\MuchLongerNameForTheFile.iso");
op.QueueDeleteOperation(shi);
op.PostDeleteItem += HandleEvent;
op.PerformOperations();
}
void HandleEvent(object sender, ShellFileOperations.ShellFileOpEventArgs args)
{
Debug.WriteLine(args);
Assert.That(args.Result.Succeeded, Is.True);
}
}
[Test]
public void NewItemTest()
{
var files = new[] { "test.docx", "test.txt", "test.xlsx" };
using (var op = new ShellFileOperations())
{
foreach (var file in files)
op.QueueNewItemOperation(ShellFolder.Desktop, file);
op.PostNewItem += HandleEvent;
op.PerformOperations();
}
foreach (var file in files)
{
var fn = Path.Combine(@"C:\Users\dahall\Desktop", file);
Assert.That(File.Exists(fn), Is.True);
File.Delete(fn);
}
void HandleEvent(object sender, ShellFileOperations.ShellFileOpEventArgs args)
{
Debug.WriteLine(args);
Assert.That(args.Result.Succeeded, Is.True);
}
}
[Test]
public void SetPropsTest2()
{
const string fn = "test.docx";
var fp = Path.Combine(ShellFolder.Desktop.FileSystemPath, fn);
var authors = new[] { "David" };
using (var p = new ShellItemPropertyUpdates { { PROPERTYKEY.System.Author, authors } })
using (var op = new ShellFileOperations())
{
op.PostNewItem += HandleEvent;
op.QueueNewItemOperation(ShellFolder.Desktop, fn, template: @"C:\Users\dahall\Documents\Custom Office Templates\blank.dotx");
op.QueueApplyPropertiesOperation(new ShellItem(fp), p);
op.PerformOperations();
}
Assert.That(new ShellItem(fp).Properties[PROPERTYKEY.System.Author], Is.EquivalentTo(authors));
File.Delete(fp);
void HandleEvent(object sender, ShellFileOperations.ShellFileOpEventArgs args)
{
Debug.WriteLine(args);
Assert.That(args.Result.Succeeded, Is.True);
}
}
}
}