-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cs
More file actions
151 lines (128 loc) · 5.2 KB
/
Utils.cs
File metadata and controls
151 lines (128 loc) · 5.2 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PersonalFolder {
class Utils {
public static unsafe Bitmap GrayscaleImage (Image _img, float lightness) {
var img = (Bitmap) _img.Clone();
var imgData = img.LockBits(new Rectangle(Point.Empty, img.Size), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
for (var y = 0; y < img.Height; y++) {
for (var x = 0; x < img.Width; x++) {
var i = (byte*) imgData.Scan0 + imgData.Stride * y + x * 4;
i[2] = i[1] = i[0] = (byte) ((i[2] + i[1] + i[0]) * lightness / 3);
}
}
img.UnlockBits(imgData);
return img;
}
internal static bool DirectoryExists (string path, int timeout = 100) {
var task = new Task<bool>(() => {
var info = new DirectoryInfo(path);
return info.Exists;
});
task.Start();
return task.Wait(timeout) && task.Result;
}
internal static Action OpenProgressDialog (Form parent, string text) {
var dialog = new Form();
dialog.Size = new Size(250, 120);
dialog.FormBorderStyle = FormBorderStyle.FixedToolWindow;
dialog.Text = "Выполнение...";
dialog.StartPosition = FormStartPosition.CenterParent;
var label = new Label();
label.Location = new Point(5, 5);
label.Text = "Пожалуйста, подождите...\n" + text;
label.AutoSize = false;
label.Size = new Size(225, 32);
label.TextAlign = ContentAlignment.MiddleCenter;
dialog.Controls.Add(label);
var progress = new ProgressBar();
progress.Style = ProgressBarStyle.Marquee;
progress.Step = 1;
progress.Value = 100;
progress.MarqueeAnimationSpeed = 75;
progress.Location = new Point(5, 45);
progress.Size = new Size(225, 32);
dialog.Controls.Add(progress);
var canClose = false;
dialog.FormClosing += (sender, e) => {
e.Cancel = !canClose;
};
dialog.Show(parent);
return delegate {
//dialog.Invoke
canClose = true;
dialog.Close();
};
}
internal static void HashedContentsCopy (HashAlgorithm crypto, string from, string to) {
foreach (var src in Directory.GetFiles(from)) {
var srcBuffer = File.ReadAllBytes(src);
var srcHash = crypto.ComputeHash(srcBuffer);
var dest = to + "\\" + Path.GetFileName(src);
FileStream destStream;
try {
destStream = File.OpenRead(dest);
} catch (FileNotFoundException) {
File.WriteAllBytes(dest, srcBuffer);
continue;
}
var destHash = crypto.ComputeHash(destStream);
destStream.Dispose();
for (var i = 0; i < crypto.HashSize >> 3; i++) {
if (srcHash[i] != destHash[i]) {
File.WriteAllBytes(dest, srcBuffer);
break;
}
}
}
foreach (var src in Directory.GetDirectories(from)) {
var dest = to + "\\" + Path.GetFileName(src);
if (!Directory.Exists(dest)) {
Directory.CreateDirectory(dest);
}
HashedContentsCopy(crypto, src, dest);
}
}
internal static bool IsFilesEquals (HashAlgorithm crypto, string fileA, string fileB) {
var streamA = File.OpenRead(fileA);
var hashA = crypto.ComputeHash(streamA);
streamA.Dispose();
var streamB = File.OpenRead(fileB);
var hashB = crypto.ComputeHash(streamB);
streamB.Dispose();
for (var i = 0; i < crypto.HashSize; i++) {
if (hashA[i] != hashB[i])
return false;
}
return true;
}
internal static bool MoveMerge (string localPath, string remotePath) {
var isRemove = true;
foreach (var localDir in Directory.GetDirectories(localPath)) {
var dirName = Path.GetFileName(localDir);
if (!MoveMerge(localDir, remotePath + "\\" + dirName)) {
isRemove = false;
}
}
foreach (var localFile in Directory.GetFiles(localPath)) {
var fileName = Path.GetFileName(localFile);
try {
File.Move(localFile, remotePath + "\\" + fileName);
} catch {
isRemove = false;
}
}
try {
if (isRemove) Directory.Delete(localPath, true);
} catch {
isRemove = false;
}
return isRemove;
}
}
}