-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensions.cs
141 lines (129 loc) · 4.65 KB
/
Extensions.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
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
using NLog;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Zp
{
public static class Extensions
{
private static readonly ILogger logger = LogManager.GetCurrentClassLogger();
// Thread safe invoke.
static public void ThreadSafeControlInvoke(this Control control, Action action)
{
if (control.InvokeRequired)
{
control.Invoke(action);
}
else
{
action();
}
}
static public string GetSafePathName(this string fullFilePath, int fileRepeatIndex = 1)
{
string safeFileName = string.Empty;
if(fileRepeatIndex < 10)
{
if (File.Exists(fullFilePath))
{
string filePath = Path.GetFullPath(Path.GetDirectoryName(fullFilePath));
string fileName = Path.GetFileNameWithoutExtension(fullFilePath);
string fileExt = Path.GetExtension(fullFilePath);
string newFileName = string.Empty;
string newFilePath;
if (fileRepeatIndex > 1)
{
newFileName = fileName.Substring(0, fileName.Length - fileRepeatIndex.ToString().Length) + fileRepeatIndex + fileExt;
logger.Info("[EXT] fileRepeatIndex name -> " + newFileName);
}
else
{
newFileName = fileName + "_" + fileRepeatIndex + fileExt;
}
newFilePath = Path.Combine(filePath, newFileName);
if (fileName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
throw new Exception("Invalid file name -> " + fileName);
return GetSafePathName(newFilePath, fileRepeatIndex + 1);
}
else
{
safeFileName = fullFilePath;
}
}
else
{
logger.Info("[EXT] File repeat was reached for " + fullFilePath);
safeFileName = fullFilePath;
}
logger.Info("[EXT] Safe file name -> " + safeFileName);
return safeFileName;
}
public static Task<T> StartSTATask<T>(Func<T> func)
{
var tcs = new TaskCompletionSource<T>();
Thread thread = new Thread(() =>
{
try
{
tcs.SetResult(func());
}
catch (Exception e)
{
tcs.SetException(e);
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
return tcs.Task;
}
public static Task StartSTATask(Action func)
{
var tcs = new TaskCompletionSource<object>();
var thread = new Thread(() =>
{
try
{
func();
tcs.SetResult(null);
}
catch (Exception e)
{
tcs.SetException(e);
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
return tcs.Task;
}
/// <summary>
/// Возвращает слова в падеже, зависимом от заданного числа
/// </summary>
/// <param name="number">Число от которого зависит выбранное слово</param>
/// <param name="nominativ">Именительный падеж слова. Например "день"</param>
/// <param name="genetiv">Родительный падеж слова. Например "дня"</param>
/// <param name="plural">Множественное число слова. Например "дней"</param>
/// <returns></returns>
public static string GetDeclension(this int number, string nominativ, string genetiv, string plural)
{
number = number % 100;
if (number >= 11 && number <= 19)
{
return plural;
}
var i = number % 10;
switch (i)
{
case 1:
return nominativ;
case 2:
case 3:
case 4:
return genetiv;
default:
return plural;
}
}
}
}