10 April 2008

A little class to help you with uri querystrings for Windows Forms



1public class URIQueryString
2{
3 string URIQuery = string.Empty;
4 Hashtable htURIQuerystring = new Hashtable();
5
6 public URIQueryString(string uriQuery)
7 {
8 this.URIQuery = uriQuery;
9 this.htURIQuerystring = Enumerate();
10 }
11
12 public Hashtable Enumerate()
13 {
14 Hashtable ret = new Hashtable();
15
16 // with HttpRequest object
17 //HttpRequest req = new HttpRequest(string.Empty, URIQuery, URIQuery.Substring(URIQuery.IndexOf('?') + 1));
18 //for (int i = 0; i < req.QueryString.Count; i++)
19 //{
20 // ret.Add(req.QueryString.GetKey(i), req.QueryString[i].ToString());
21 //}
22
23 // without HttpRequest object
24 string FullQuery = URIQuery.Substring(URIQuery.IndexOf('?') + 1);
25 string[] KV = FullQuery.Split('&');
26 for (int i = 0; i < KV.Length; i++)
27 {
28 string[] dat = KV[i].Split('=');
29 ret.Add(dat[0], dat[1]);
30 }
31
32 return ret;
33 }
34
35 public bool HasKey(string Key)
36 {
37 return htURIQuerystring.ContainsKey(Key);
38 }
39
40 public string GetURI()
41 {
42 return this.URIQuery.Substring(0, this.URIQuery.IndexOf("?"));
43 }
44
45 public void ChangeValue(string Key, string Value)
46 {
47 if (htURIQuerystring.ContainsKey(Key))
48 {
49 IDictionaryEnumerator dicEnum = htURIQuerystring.GetEnumerator();
50 while (dicEnum.MoveNext())
51 {
52 if (dicEnum.Key.ToString().ToLower() == Key.ToLower())
53 {
54 htURIQuerystring.Remove(Key);
55 htURIQuerystring.Add(Key, Value);
56
57 break;
58 }
59 }
60 }
61 }
62
63 public void RemoveKey(string Key)
64 {
65 if (htURIQuerystring.ContainsKey(Key))
66 {
67 htURIQuerystring.Remove(Key);
68 }
69 }
70
71 public string GetFullURI()
72 {
73 string ret = string.Empty;
74
75 IDictionaryEnumerator dicEnum = htURIQuerystring.GetEnumerator();
76 while (dicEnum.MoveNext())
77 {
78 ret += string.Format("{0}={1}&", dicEnum.Key, dicEnum.Value);
79 }
80
81 return GetURI() + "?" + ret.Remove(ret.Length - 1, 1);
82 }
83}

No comments: