This repository has been archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Extensions.cs
158 lines (138 loc) · 5.06 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using IisLogRotator.Resources;
using Microsoft.Web.Administration;
using System;
using System.DirectoryServices;
using System.Globalization;
using System.Linq;
namespace IisLogRotator
{
internal static class Extensions
{
public static string GetAttributeValue2(this ConfigurationElement element, string name)
{
ConfigurationAttribute attr = element.Attributes.FirstOrDefault(a => a.Name == name);
if (attr == null)
return "<not found>";
if (attr.Value == null)
return "<null>";
if (attr.Value.Equals(string.Empty))
return "<empty>";
return "\"" + attr.Value.ToString() + "\"";
}
public static int GetWeekOfMonth(this DateTime date)
{
if (date.Month == 1)
{
return date.GetWeekOfYear();
}
else
{
DateTime first = new DateTime(date.Year, date.Month, 1);
return date.GetWeekOfYear() - first.GetWeekOfYear() + 1;
}
}
public static int GetWeekOfYear(this DateTime date)
{
DateTimeFormatInfo dtfi = DateTimeFormatInfo.CurrentInfo;
return dtfi.Calendar.GetWeekOfYear(
date,
dtfi.CalendarWeekRule,
dtfi.FirstDayOfWeek
);
}
public static string StripeUtf8Prefix(this string str)
{
if (str.StartsWith("u_", StringComparison.Ordinal))
{
return str.Substring(2);
}
return str;
}
public static T GetPropertyValue<T>(this DirectoryEntry entry, string propertyName)
{
return (T)entry.Properties[propertyName].Value;
}
public static T GetPropertyValue<T>(this DirectoryEntry entry, string propertyName, T defaultValue)
{
if (entry.Properties.Contains(propertyName))
{
return (T)entry.Properties[propertyName].Value;
}
return defaultValue;
}
public static string GetIisLegacyVersionString(this OperatingSystem os)
{
switch (os.Version.Major)
{
case 5:
switch (os.Version.Minor)
{
case 0: return "IIS 5.0";
case 1: return "IIS 5.1";
case 2: return "IIS 6.0";
}
break;
case 6:
switch (os.Version.Minor)
{
case 0:
case 1:
case 2:
return "IIS 6.0";
}
break;
}
return Strings.MsgUnknownIisVersionString;
}
public static string GetIisVersionString(this OperatingSystem os)
{
switch (os.Version.Major)
{
case 5:
switch (os.Version.Minor)
{
case 0: return "IIS 5.0";
case 1: return "IIS 5.1";
case 2: return "IIS 6.0";
}
break;
case 6:
switch (os.Version.Minor)
{
case 0: return "IIS 7.0";
case 1: return "IIS 7.5";
case 2: return "IIS 8.0";
case 3: return "IIS 8.5";
}
break;
case 10:
switch (os.Version.Minor)
{
case 0: return "IIS 10.0";
}
break;
}
return Strings.MsgUnknownIisVersionString;
}
public static bool LessThan(this OperatingSystem os, int major, int minor)
{
return (os.Version.Major < major || (os.Version.Major == major && os.Version.Minor < minor));
}
public static bool LessThanEqual(this OperatingSystem os, int major, int minor)
{
return (os.Version.Major < major || (os.Version.Major == major && os.Version.Minor <= minor));
}
public static bool Equal(this OperatingSystem os, int major, int minor)
{
return (os.Version.Major == major && os.Version.Minor == minor);
}
public static bool GreaterThan(this OperatingSystem os, int major, int minor)
{
return (os.Version.Major > major || (os.Version.Major == major && os.Version.Minor > minor));
}
public static bool GreaterThanEqual(this OperatingSystem os, int major, int minor)
{
return (os.Version.Major > major || (os.Version.Major == major && os.Version.Minor >= minor));
}
}
}