-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathUtil.cs
76 lines (63 loc) · 2.78 KB
/
Util.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Web;
namespace Lanayo.Vagrant_Manager {
public static class Util {
public static string EscapeShellArg(string arg) {
return "\"" + Regex.Replace(arg, @"(\\+)$", @"$1$1") + "\"";
}
public static string TrimWhitespaceAndNewlines(string str) {
return str.Trim(Environment.NewLine.ToCharArray().Concat(new char[] { ' ' }).ToArray());
}
public static ToolStripMenuItem MakeBlankToolstripMenuItem(string Name, EventHandler onClick) {
ToolStripMenuItem menuItem = new ToolStripMenuItem(Name);
menuItem.Click += onClick;
return menuItem;
}
public static Uri AddQuery(this Uri uri, string name, string value) {
var ub = new UriBuilder(uri);
// decodes urlencoded pairs from uri.Query to HttpValueCollection
var httpValueCollection = HttpUtility.ParseQueryString(uri.Query);
httpValueCollection.Add(name, value);
// this code block is taken from httpValueCollection.ToString() method
// and modified so it encodes strings with HttpUtility.UrlEncode
if (httpValueCollection.Count == 0)
ub.Query = String.Empty;
else {
var sb = new StringBuilder();
for (int i = 0; i < httpValueCollection.Count; i++) {
string text = httpValueCollection.GetKey(i);
{
text = HttpUtility.UrlEncode(text);
string val = (text != null) ? (text + "=") : string.Empty;
string[] vals = httpValueCollection.GetValues(i);
if (sb.Length > 0)
sb.Append('&');
if (vals == null || vals.Length == 0)
sb.Append(val);
else {
if (vals.Length == 1) {
sb.Append(val);
sb.Append(HttpUtility.UrlEncode(vals[0]));
} else {
for (int j = 0; j < vals.Length; j++) {
if (j > 0)
sb.Append('&');
sb.Append(val);
sb.Append(HttpUtility.UrlEncode(vals[j]));
}
}
}
}
}
ub.Query = sb.ToString();
}
return ub.Uri;
}
}
}