Subversion Repository Public Repository

CommonUtility

Diff Revisions 11 vs 12 for /CommonUtility/CommonUtility/FTPUtility.cs

Diff revisions: vs.
  @@ -16,9 +16,17 @@
16 16 {
17 17 private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
18 18 private string _connectionString;
19 - public FTPUtility(string connectionString)
19 + private Session _session;
20 +
21 + public FTPUtility(string connectionString, string FTPHost, string username)
20 22 {
21 23 _connectionString = connectionString;
24 +
25 + if (_session == null)
26 + {
27 + _session = new Session();
28 + _session.Open(GetSessionOptions(FTPHost, username));
29 + }
22 30 }
23 31
24 32 public void GetFile(string remoteFilePath, string localFilePath, string FTPHost, string username, bool delete_after)
  @@ -27,26 +35,18 @@
27 35 {
28 36 Console.WriteLine("\n FTPUTIL: Get File: " + remoteFilePath);
29 37
30 - using (Session session = new Session())
31 - {
32 - // Connect
33 - session.Open(GetSessionOptions(FTPHost, username));
34 - session.
35 -
36 - // Upload files
37 - TransferOptions transferOptions = new TransferOptions();
38 - transferOptions.TransferMode = TransferMode.Binary;
39 - transferOptions.PreserveTimestamp = true;
40 -
41 - session.GetFiles(remoteFilePath, localFilePath, delete_after, transferOptions).Check();
38 + // Upload files
39 + TransferOptions transferOptions = new TransferOptions();
40 + transferOptions.TransferMode = TransferMode.Binary;
41 + transferOptions.PreserveTimestamp = true;
42 42
43 - session.Dispose();
43 + _session.GetFiles(remoteFilePath, localFilePath, delete_after, transferOptions).Check();
44 44
45 - }
46 45 }
47 46 catch (Exception e)
48 47 {
49 48 log.Error("Exception retrieving file: " + remoteFilePath + ". \n FTP Host: " + FTPHost + "\n Username: " + username + " \n Exception: " + e.Message);
49 + _session.Dispose();
50 50 throw new Exception("FTPUtility EXCEPTION(GetFile()): " + e.Message);
51 51 }
52 52 }
  @@ -59,27 +59,21 @@
59 59 {
60 60 Console.WriteLine("\n FTPUTIL: Get File: " + remoteFilePath);
61 61
62 - using (Session session = new Session())
63 - {
64 - // Connect
65 - session.Open(GetSessionOptions(FTPHost, username));
66 -
67 - // Upload files
68 - TransferOptions transferOptions = new TransferOptions();
69 - transferOptions.TransferMode = TransferMode.Binary;
70 - transferOptions.PreserveTimestamp = true;
71 -
72 - RemoteDirectoryInfo directory = session.ListDirectory(remoteFilePath);
73 -
74 - foreach (RemoteFileInfo file in directory.Files)
75 - fileList.Add(file.Name);
76 -
77 - session.Dispose();
78 - }
62 + // Upload files
63 + TransferOptions transferOptions = new TransferOptions();
64 + transferOptions.TransferMode = TransferMode.Binary;
65 + transferOptions.PreserveTimestamp = true;
66 +
67 + RemoteDirectoryInfo directory = _session.ListDirectory(remoteFilePath);
68 +
69 + foreach (RemoteFileInfo file in directory.Files)
70 + fileList.Add(file.Name);
71 +
79 72 }
80 73 catch (Exception e)
81 74 {
82 75 log.Error("Exception listing files in directory: " + remoteFilePath + ". \n FTP Host: " + FTPHost + "\n Username: " + username + " \n Exception: " + e.Message);
76 + _session.Dispose();
83 77 throw new Exception("FTPUtility EXCEPTION(ListFilesInDirectory()): " + e.Message);
84 78 }
85 79
  @@ -90,57 +84,48 @@
90 84 {
91 85 try
92 86 {
93 - using (Session session = new Session())
94 - {
95 - Console.WriteLine("\n FTPUTIL: Put File: " + remoteFilePath);
96 87
97 - // Connect
98 - session.Open(GetSessionOptions(FTPHost, username));
88 + Console.WriteLine("\n FTPUTIL: Put File: " + remoteFilePath);
99 89
100 - // Upload files
101 - TransferOptions transferOptions = new TransferOptions();
102 - transferOptions.TransferMode = TransferMode.Binary;
90 + // Upload files
91 + TransferOptions transferOptions = new TransferOptions();
92 + transferOptions.TransferMode = TransferMode.Binary;
103 93
104 - session.PutFiles(localFilepath, remoteFilePath, false, transferOptions).Check();
105 -
106 - session.Dispose();
107 - }
94 + _session.PutFiles(localFilepath, remoteFilePath, false, transferOptions).Check();
95 +
108 96 }
109 97 catch (Exception e)
110 98 {
111 99 log.Error("Exception putting file: " + remoteFilePath + ".\n FTP Host: " + FTPHost + "\n Username: " + username + " \n Exception: " + e.Message);
100 + _session.Dispose();
112 101 throw new Exception("FTPUtility EXCEPTION(PutFile()): " + e.Message);
113 102 }
114 103 }
115 104
116 105 public void MoveFile(string sourceFilePath, string targetFilePath, string FTPHost, string username, bool delete_after)
117 106 {
118 - using (Session session = new Session())
107 +
108 + try
119 109 {
120 - try
121 - {
122 - Console.WriteLine("\n FTPUTIL: Move File: " + targetFilePath);
123 -
124 - // Connect
125 - session.Open(GetSessionOptions(FTPHost, username));
110 + Console.WriteLine("\n FTPUTIL: Move File: " + targetFilePath);
126 111
127 - // Upload files
128 - TransferOptions transferOptions = new TransferOptions();
129 - transferOptions.TransferMode = TransferMode.Binary;
112 + // Upload files
113 + TransferOptions transferOptions = new TransferOptions();
114 + transferOptions.TransferMode = TransferMode.Binary;
130 115
131 - session.MoveFile(sourceFilePath, targetFilePath);
116 + _session.MoveFile(sourceFilePath, targetFilePath);
132 117
133 - if (delete_after)
134 - session.RemoveFiles(sourceFilePath);
118 + if (delete_after)
119 + _session.RemoveFiles(sourceFilePath);
135 120
136 - session.Dispose();
137 - }
138 - catch (Exception e)
139 - {
140 - log.Error("Exception putting file: " + targetFilePath + ".\n FTP Host: " + FTPHost + "\n Username: " + username + " \n Exception: " + e.Message);
141 - throw new Exception("FTPUtility EXCEPTION(MoveFile()): " + e.Message);
142 - }
143 121 }
122 + catch (Exception e)
123 + {
124 + log.Error("Exception putting file: " + targetFilePath + ".\n FTP Host: " + FTPHost + "\n Username: " + username + " \n Exception: " + e.Message);
125 + _session.Dispose();
126 + throw new Exception("FTPUtility EXCEPTION(MoveFile()): " + e.Message);
127 + }
128 +
144 129 }
145 130
146 131 public bool DirExists(string remoteDirectory, string FTPHost, string username)
  @@ -149,21 +134,17 @@
149 134
150 135 try
151 136 {
152 - using (Session session = new Session())
153 - {
154 - // Connect
155 - session.Open(GetSessionOptions(FTPHost, username));
156 -
157 - // Upload files
158 - TransferOptions transferOptions = new TransferOptions();
159 - transferOptions.TransferMode = TransferMode.Binary;
137 + // Upload files
138 + TransferOptions transferOptions = new TransferOptions();
139 + transferOptions.TransferMode = TransferMode.Binary;
160 140
161 - ret = session.FileExists(remoteDirectory);
162 - }
141 + ret = _session.FileExists(remoteDirectory);
142 +
163 143 }
164 144 catch (Exception e)
165 145 {
166 146 log.Error("Exception checking if directory exists: " + remoteDirectory + ".\n FTP Host: " + FTPHost + "\n Username: " + username + " \n Exception: " + e.Message);
147 + _session.Dispose();
167 148 throw new Exception("FTPUtility EXCEPTION(DirExists()): " + e.Message);
168 149 }
169 150
  @@ -174,21 +155,17 @@
174 155 {
175 156 try
176 157 {
177 - using (Session session = new Session())
178 - {
179 - // Connect
180 - session.Open(GetSessionOptions(FTPHost, username));
181 -
182 - // Upload files
183 - TransferOptions transferOptions = new TransferOptions();
184 - transferOptions.TransferMode = TransferMode.Binary;
158 + // Upload files
159 + TransferOptions transferOptions = new TransferOptions();
160 + transferOptions.TransferMode = TransferMode.Binary;
185 161
186 - session.CreateDirectory(remoteDirectory);
187 - }
162 + _session.CreateDirectory(remoteDirectory);
163 +
188 164 }
189 165 catch (Exception e)
190 166 {
191 167 log.Error("Exception creating directory: " + remoteDirectory + ".\n FTP Host: " + FTPHost + "\n Username: " + username + " \n Exception: " + e.Message);
168 + _session.Dispose();
192 169 throw new Exception("FTPUtility EXCEPTION(CreateDir()): " + e.Message);
193 170 }
194 171
  @@ -202,24 +179,18 @@
202 179
203 180 try
204 181 {
205 - using (Session session = new Session())
206 - {
207 - // Connect
208 - session.Open(GetSessionOptions(FTPHost, username));
209 -
210 - // Upload files
211 - TransferOptions transferOptions = new TransferOptions();
212 - transferOptions.TransferMode = TransferMode.Binary;
182 + // Upload files
183 + TransferOptions transferOptions = new TransferOptions();
184 + transferOptions.TransferMode = TransferMode.Binary;
213 185
214 - ret = session.FileExists(remoteFilePath);
215 -
216 - }
186 + ret = _session.FileExists(remoteFilePath);
217 187
218 188 Console.WriteLine("\n FileExists SUCCESS");
219 189 }
220 190 catch (Exception e)
221 191 {
222 192 log.Error("Exception checking if file exists at location: " + remoteFilePath + ".\n FTP Host: " + FTPHost + "\n Username: " + username + " \n Exception: " + e.Message);
193 + _session.Dispose();
223 194 throw new Exception("FTPUtility EXCEPTION(FileExists()): " + e.Message);
224 195 }
225 196
  @@ -248,39 +219,31 @@
248 219
249 220 if (FileExists(remoteFilePath, FTPHost, username)) // only check if file already exists
250 221 {
222 + // Upload files
223 + TransferOptions transferOptions = new TransferOptions();
224 + transferOptions.TransferMode = TransferMode.Binary;
225 + transferOptions.PreserveTimestamp = true;
251 226
252 - using (Session session = new Session())
227 + while (current_try <= max_retry) // Max 10 Trys
253 228 {
254 - // Connect
255 - session.Open(GetSessionOptions(FTPHost, username));
256 -
257 - // Upload files
258 - TransferOptions transferOptions = new TransferOptions();
259 - transferOptions.TransferMode = TransferMode.Binary;
260 - transferOptions.PreserveTimestamp = true;
261 -
262 - while (current_try <= max_retry) // Max 10 Trys
263 - {
264 - session.GetFiles(remoteFilePath, localFilePath, delete_after, transferOptions).Check();
265 - byte[] current_file = File.ReadAllBytes(localFilePath);
266 - current_filesize = current_file.Length;
267 -
268 - if (previous_filesize == current_filesize && current_filesize != 0)
269 - break;
270 -
271 - current_try++;
272 - previous_filesize = current_filesize;
273 - Thread.Sleep(3000); // Wait 3 seconds to try again.
274 - }
275 -
276 - session.Dispose();
277 -
229 + _session.GetFiles(remoteFilePath, localFilePath, delete_after, transferOptions).Check();
230 + byte[] current_file = File.ReadAllBytes(localFilePath);
231 + current_filesize = current_file.Length;
232 +
233 + if (previous_filesize == current_filesize && current_filesize != 0)
234 + break;
235 +
236 + current_try++;
237 + previous_filesize = current_filesize;
238 + Thread.Sleep(1000); // Wait 3 seconds to try again.
278 239 }
279 240 }
241 +
280 242 }
281 243 catch (Exception e)
282 244 {
283 245 log.Error("Exception retrieving full file: " + remoteFilePath + ". \n FTP Host: " + FTPHost + "\n Username: " + username + " \n Exception: " + e.Message);
246 + _session.Dispose();
284 247 throw new Exception("FTPUtility EXCEPTION(GetFile()): " + e.Message);
285 248 }
286 249 }
  @@ -294,33 +257,28 @@
294 257
295 258 try
296 259 {
297 - using (Session session = new Session())
298 - {
299 - // Connect
300 - session.Open(GetSessionOptions(FTPHost, username));
301 -
302 - // Upload files
303 - TransferOptions transferOptions = new TransferOptions();
304 - transferOptions.TransferMode = TransferMode.Binary;
260 + // Upload files
261 + TransferOptions transferOptions = new TransferOptions();
262 + transferOptions.TransferMode = TransferMode.Binary;
305 263
306 - RemoteDirectoryInfo directory_info = session.ListDirectory(remoteFilePath);
264 + RemoteDirectoryInfo directory_info = _session.ListDirectory(remoteFilePath);
307 265
308 - DateTime last_modified = new DateTime();
309 - foreach (RemoteFileInfo fileinfo in directory_info.Files)
266 + DateTime last_modified = new DateTime();
267 + foreach (RemoteFileInfo fileinfo in directory_info.Files)
268 + {
269 + if (fileinfo.Name.Contains(file_name_pattern) && fileinfo.LastWriteTime > last_modified)
310 270 {
311 - if (fileinfo.Name.Contains(file_name_pattern) && fileinfo.LastWriteTime > last_modified)
312 - {
313 - filename = fileinfo.Name;
314 - last_modified = fileinfo.LastWriteTime;
315 - }
271 + filename = fileinfo.Name;
272 + last_modified = fileinfo.LastWriteTime;
316 273 }
317 274 }
318 -
275 +
319 276 Console.WriteLine("\n FileExists SUCCESS");
320 277 }
321 278 catch (Exception e)
322 279 {
323 280 log.Error("Exception checking if file exists at location: " + remoteFilePath + ".\n FTP Host: " + FTPHost + "\n Username: " + username + " \n Exception: " + e.Message);
281 + _session.Dispose();
324 282 throw new Exception("FTPUtility EXCEPTION(FileExists()): " + e.Message);
325 283 }
326 284
  @@ -338,32 +296,27 @@
338 296
339 297 try
340 298 {
341 - using (Session session = new Session())
342 - {
343 - // Connect
344 - session.Open(GetSessionOptions(FTPHost, username));
299 + // Upload files
300 + TransferOptions transferOptions = new TransferOptions();
301 + transferOptions.TransferMode = TransferMode.Binary;
345 302
346 - // Upload files
347 - TransferOptions transferOptions = new TransferOptions();
348 - transferOptions.TransferMode = TransferMode.Binary;
303 + RemoteDirectoryInfo directory_info = _session.ListDirectory(remoteFilePath);
349 304
350 - RemoteDirectoryInfo directory_info = session.ListDirectory(remoteFilePath);
351 -
352 - foreach (RemoteFileInfo fileinfo in directory_info.Files)
305 + foreach (RemoteFileInfo fileinfo in directory_info.Files)
306 + {
307 + if (fileinfo.Name.Contains(file_name_pattern) && fileinfo.LastWriteTime > last_modified)
353 308 {
354 - if (fileinfo.Name.Contains(file_name_pattern) && fileinfo.LastWriteTime > last_modified)
355 - {
356 - filename = fileinfo.Name;
357 - last_modified = fileinfo.LastWriteTime;
358 - }
309 + filename = fileinfo.Name;
310 + last_modified = fileinfo.LastWriteTime;
359 311 }
360 312 }
361 -
313 +
362 314 Console.WriteLine("\n FileExists SUCCESS");
363 315 }
364 316 catch (Exception e)
365 317 {
366 318 log.Error("Exception checking if file exists at location: " + remoteFilePath + ".\n FTP Host: " + FTPHost + "\n Username: " + username + " \n Exception: " + e.Message);
319 + _session.Dispose();
367 320 throw new Exception("FTPUtility EXCEPTION(FileExists()): " + e.Message);
368 321 }
369 322
  @@ -412,5 +365,10 @@
412 365
413 366 return sessionOptions;
414 367 }
368 +
369 + public void CloseSession()
370 + {
371 + _session.Dispose();
372 + }
415 373 }
416 374 }