1: public class PhotoData
2: {
3: #region Declarations and properties
4:
5: //the directory that holds the photos
6: private List<aphotoCatagory> CatagoryLists = new List<aphotoCatagory>();
7:
8: private String _PhotoDirectory;
9: #endregion
10:
11: #region Private Class Methods
12: private void makeCatagoryList(string PhotoDirectory)
13: {
14: try
15: {
16: if (Directory.Exists(HttpContext.Current.Server.MapPath(PhotoDirectory)))
17: {
18: string[] aryDirectories =
19: Directory.GetDirectories(HttpContext.Current.Server.MapPath(PhotoDirectory));
20:
21: string[] files;
22: string[] ExclusionDirectories = { "_vti_cnf", "_vti_pvt", "_vti_script" };
23:
24: //test for front page extensions and remove them
25: for (int i = 0; i < aryDirectories.Length; i++)
26: {
27: //test for folder with _vt_cnf , _vt_pvt _vti_script
28: foreach (string j in ExclusionDirectories)
29: {
30:
31: if (aryDirectories[i].Contains(j))
32: {
33: aryDirectories[i] = "";
34: } //end if
35: } //end for each
36: } //end for
37: int ab = aryDirectories.Length;
38:
39: //test for condition that no subfolders found
40: if (aryDirectories.Length == 1 && aryDirectories[0] == "")
41: {
42: files = Directory.GetFiles(HttpContext.Current.Server.MapPath(PhotoDirectory));
43: if (files.Length >= 1) //if not empty
44: {
45: aphotoCatagory cat = new aphotoCatagory();
46: string keyname = returnCatagoryName(HttpContext.Current.Server.MapPath(PhotoDirectory));
47: cat.Cname = keyname;
48: cat.Cpath = HttpContext.Current.Server.MapPath(PhotoDirectory);
49: PhotoItems(keyname, ref cat, false); // fill the object up
50: CatagoryLists.Add(cat); //add the catagory to the list;
51: } //end if
52:
53: }
54: else
55: {
56: for (int i = 0; i < aryDirectories.Length; i++)
57: {
58: files = Directory.GetFiles(aryDirectories[i]); //test for empty directories
59: if (files.Length >= 1) //if not empty
60: {
61: aphotoCatagory cat = new aphotoCatagory();
62: string keyname = returnCatagoryName(aryDirectories[i]);
63: cat.Cname = keyname;
64: cat.Cpath = aryDirectories[i];
65: PhotoItems(keyname, ref cat, true); // fill the object up
66: CatagoryLists.Add(cat); //add the catagory to the list;
67: } //end if
68: }//end for
69: }//end if
70:
71: }//end if
72: }
73: catch
74: {
75: //just produce null wont crash the Web application
76: }
77: } //end of makeCatagoryList()
78:
79: private void PhotoItems(string key, ref aphotoCatagory cat, Boolean subs)
80: {
81: string[] aryDirectories = null;
82: string keys;
83:
84: //combine the relative path and the key Catagory Name: 'Play'
85: if (subs)
86: {
87: keys = Path.Combine(_PhotoDirectory, key);
88:
89: //get the files in the directory returns c:\\Directory\\Directory\\Directory\\Directory\\FileName.jpg'
90: //use mappath change our relative path to application root.
91: aryDirectories = Directory.GetFiles(HttpContext.Current.Server.MapPath(keys), "*.jpg");
92:
93: //make the path relative not the full c:\\Directory\\Directory\\Directory\\Directory\\FileName.jpg'
94: for (int i = 0; i < aryDirectories.Length; i++)
95: {
96: //return a file name from the full path e.g 'image01.jpg'
97: string str = Path.GetFileName(aryDirectories[i]);
98: //combine our relative path and with the catagory name.
99: string temp = Path.Combine(_PhotoDirectory, this.returnCatagoryName(aryDirectories[i]));
100: //combine the filename and the relative path (cant use path.combine)
101: aryDirectories[i] = temp + '/' + str;
102: cat.Add(aryDirectories[i]);
103: } //end for
104: }
105: else
106: {
107: keys = _PhotoDirectory;
108:
109: //get the files in the directory returns
110: //c:\\Directory\\Directory\\Directory\\Directory\\FileName.jpg'
111: //use mappath change our relative path to application root.
112: aryDirectories = Directory.GetFiles(HttpContext.Current.Server.MapPath(keys), "*.jpg");
113:
114: //make the path relative not the full
115: //c:\\Directory\\Directory\\Directory\\Directory\\FileName.jpg'
116: for (int i = 0; i < aryDirectories.Length; i++)
117: {
118: string str = Path.GetFileName(aryDirectories[i]);
119: aryDirectories[i] = Path.Combine( _PhotoDirectory , str);
120: cat.Add(aryDirectories[i]);
121: } //end fir
122:
123: }//end if
124:
125:
126: } //end of PhototItrms
127:
128: #endregion
129:
130: #region public Methods
131: public int count()
132: {
133: return CatagoryLists.Count;
134: }
135:
136: public string returnCatagoryName(string dpath)
137: {
138:
139: string[] nameary; //temp var
140: nameary = dpath.Split(Path.DirectorySeparatorChar);
141: string CatName = null;
142:
143: if (Path.HasExtension(dpath))
144: {
145: CatName = nameary[nameary.GetUpperBound(0) - 1];
146: }//end if
147: else
148: {
149: CatName = nameary[nameary.GetUpperBound(0)];
150: }//end else
151: return CatName;
152: } //end of returnCatagoryName
153:
154:
155: public aphotoCatagory getItem(int item)
156: {
157: if (item < CatagoryLists.Count && item >= 0)
158: {
159: return CatagoryLists[item];
160:
161: }
162: else
163: {
164: return null;
165: }
166:
167: } //end of getitem
168:
169: #endregion
170:
171: #region constructor
172: public PhotoData(string directory)
173: {
174: _PhotoDirectory = directory; //set the state variable
175: makeCatagoryList(_PhotoDirectory); //fill the dictionary object
176:
177: }
178: #endregion
179:
180: } //end of class