00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 using System;
00013 using System.Globalization;
00014 using System.IO;
00015 using System.Net;
00016 using System.Text.RegularExpressions;
00017 using System.Windows.Forms;
00018 using ICSharpCode.SharpZipLib.Zip;
00019 using Mcs.Epm.MicrosoftProject.mpFx.Client.Shared.Properties;
00020
00021 namespace Mcs.Epm.MicrosoftProject.mpFx.Client.Shared
00022 {
00023
00024
00025
00026 public class Tools
00027 {
00028 #region Private Constants
00029
00030 private const int BUFFER_SIZE = 16;
00031
00032 #endregion
00033
00034 #region Guid Routines
00035
00036 public static bool IsGuid(string candidate)
00037 {
00038 bool isValid = false;
00039
00040 if (!string.IsNullOrEmpty(candidate))
00041 {
00042 Regex isGuid = new Regex(@"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$", RegexOptions.Compiled);
00043
00044 if (isGuid.IsMatch(candidate))
00045 {
00046 isValid = true;
00047 }
00048 }
00049
00050 return isValid;
00051
00052 }
00053
00054 #endregion
00055
00056 #region Validation Routines
00057
00058 public static void ValidateOnLoadParams(ProjectServer projectServer, Form parent, ToolStrip toolStrip)
00059 {
00060 ValidateObjectParam(projectServer, Resources.NullProjectServerObject);
00061 ValidateObjectParam(parent, Resources.NullParentFormObject);
00062 ValidateObjectParam(toolStrip, Resources.NullToolStripObject);
00063 }
00064
00065 public static void ValidateObjectParam(object validateObject, string errorMessage)
00066 {
00067 if (validateObject == null)
00068 {
00069 throw new ArgumentNullException(errorMessage);
00070 }
00071 }
00072
00073 public static void ValidateDirectoryParam(string directory, string errorMessage)
00074 {
00075 if (string.IsNullOrEmpty(directory) || !Directory.Exists(directory))
00076 {
00077 throw new ArgumentException(errorMessage);
00078 }
00079 }
00080
00081 public static bool DoesExistInGridView(DataGridView dataGridView, int columnIndex, string text)
00082 {
00083 foreach (DataGridViewRow row in dataGridView.Rows)
00084 {
00085 if (row.IsNewRow)
00086 {
00087 continue;
00088 }
00089 if (row.Cells[columnIndex].Value.ToString().Equals(text, StringComparison.CurrentCultureIgnoreCase))
00090 {
00091 return true;
00092 }
00093 }
00094
00095 return false;
00096
00097 }
00098
00099 #endregion
00100
00101 #region WinForms Cleanup
00102
00103 public static void DisposeForm(Form form)
00104 {
00105 if (form != null && !form.IsDisposed)
00106 {
00107 form.Close();
00108 form.Dispose();
00109 }
00110 }
00111
00112 public static ToolStripDropDownButton FindToolStripDropDownButtonByText(ToolStrip toolStrip, string text)
00113 {
00114 text = text.Replace("&", "");
00115
00116 foreach (object item in toolStrip.Items)
00117 {
00118 ToolStripDropDownButton toolStripDropDownButton = item as ToolStripDropDownButton;
00119
00120 if (toolStripDropDownButton != null)
00121 {
00122 string tempText = toolStripDropDownButton.Text.Replace("&", "");
00123
00124 if (tempText.Equals(text, StringComparison.CurrentCultureIgnoreCase))
00125 {
00126 return toolStripDropDownButton;
00127 }
00128 }
00129 }
00130
00131 return null;
00132 }
00133
00134 public static void CleanupToolStripItem(ToolStripItemCollection items, ToolStripMenuItem item)
00135 {
00136 if (item != null)
00137 {
00138 items.Remove(item);
00139 item.Dispose();
00140 }
00141 }
00142
00143 public static void CleanupToolStripItem(ToolStripItemCollection items, ToolStripSeparator separator)
00144 {
00145 if (separator != null)
00146 {
00147 items.Remove(separator);
00148 separator.Dispose();
00149 }
00150 }
00151
00152 public static void CleanupToolStripItem(ToolStripItemCollection items, ToolStripDropDownButton button)
00153 {
00154 if (button != null)
00155 {
00156 items.Remove(button);
00157 button.Dispose();
00158 }
00159 }
00160
00161 #endregion
00162
00163 #region String Routines
00164
00165 public static string GetDecameledString(string cameledString)
00166 {
00167 int lastUpper = -1;
00168
00169 string displayName = string.Empty;
00170
00171 for (int i = 0; i < cameledString.Length; i++)
00172 {
00173 bool isUpper = char.IsUpper(cameledString[i]);
00174
00175 if (isUpper && i > 0 && i - lastUpper > 1)
00176 {
00177 displayName = string.Format("{0} {1}", displayName, cameledString[i]);
00178 }
00179 else
00180 {
00181 displayName = displayName + cameledString[i];
00182 }
00183
00184 if (isUpper)
00185 {
00186 lastUpper = i;
00187 }
00188 }
00189
00190 return displayName;
00191 }
00192
00193 public static string Format(string text, params object[] args)
00194 {
00195 return string.Format(CultureInfo.CurrentCulture, text, args);
00196 }
00197
00198 #endregion
00199
00200 #region Http Wrappers
00201
00202 public static void HttpDownload(string url, string target)
00203 {
00204 WebRequest request = WebRequest.Create(url);
00205
00206 if (request == null)
00207 {
00208
00209 throw null;
00210 }
00211
00212 WebResponse response = request.GetResponse();
00213
00214 if (response == null)
00215 {
00216
00217 throw null;
00218 }
00219
00220 using (Stream responseStream = response.GetResponseStream())
00221 {
00222 using (FileStream localCopy = File.OpenWrite(target))
00223 {
00224 byte[] Buffer = new byte[BUFFER_SIZE];
00225 int read;
00226
00227 do
00228 {
00229 read = responseStream.Read(Buffer, 0, Buffer.Length);
00230 localCopy.Write(Buffer, 0, read);
00231 Application.DoEvents();
00232 }
00233 while (read > 0);
00234
00235 localCopy.Flush();
00236 localCopy.Close();
00237 }
00238
00239 responseStream.Close();
00240 }
00241 }
00242
00243 #endregion
00244
00245 #region Ftp Wrappers
00246
00247 public static bool FtpRemoveDirectory(string ftpLocation, string username, string password)
00248 {
00249
00250 FtpWebRequest listRequest = (FtpWebRequest)WebRequest.Create(ftpLocation.TrimEnd('/'));
00251 listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
00252 listRequest.Credentials = new NetworkCredential(username, password);
00253
00254 using (FtpWebResponse removeResponse = (FtpWebResponse)listRequest.GetResponse())
00255 {
00256 using (Stream removeResponseStream = removeResponse.GetResponseStream())
00257 using (StreamReader removeReader = new StreamReader(removeResponseStream))
00258 {
00259 string listDetails = removeReader.ReadToEnd();
00260
00261 if (string.IsNullOrEmpty(listDetails) || listDetails.Contains("total 0"))
00262 {
00263
00264 }
00265 else
00266 {
00267 FtpRemoveDirectoryFiles(ftpLocation, username, password);
00268 }
00269 }
00270 }
00271
00272 FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpLocation.TrimEnd('/'));
00273 request.Method = WebRequestMethods.Ftp.RemoveDirectory;
00274 request.Credentials = new NetworkCredential(username, password);
00275
00276 using (FtpWebResponse removeResponse = (FtpWebResponse)request.GetResponse())
00277 {
00278 using (Stream removeResponseStream = removeResponse.GetResponseStream())
00279 using (StreamReader removeReader = new StreamReader(removeResponseStream))
00280 {
00281 string removeDetails = removeReader.ReadToEnd();
00282 return string.IsNullOrEmpty(removeDetails);
00283 }
00284 }
00285 }
00286
00287 public static void FtpRemoveDirectoryFiles(string location, string username, string password)
00288 {
00289 FtpWebRequest request = (FtpWebRequest)WebRequest.Create(location.TrimEnd('/'));
00290 request.Method = WebRequestMethods.Ftp.ListDirectory;
00291 request.Credentials = new NetworkCredential(username, password);
00292
00293 using (FtpWebResponse removeResponse = (FtpWebResponse)request.GetResponse())
00294 {
00295 using (Stream removeResponseStream = removeResponse.GetResponseStream())
00296 using (StreamReader removeReader = new StreamReader(removeResponseStream))
00297 {
00298 string listDetails = removeReader.ReadToEnd();
00299
00300 if (!location.EndsWith("/"))
00301 {
00302 location = location + "/";
00303 }
00304
00305 string[] files = listDetails.Split('\n');
00306
00307 foreach (string file in files)
00308 {
00309 if (!string.IsNullOrEmpty(file))
00310 {
00311 string fullPath = location + file.Substring(file.IndexOf('/') + 1);
00312
00313 FtpRemoveFile(fullPath, username, password);
00314 }
00315 }
00316 }
00317 }
00318 }
00319
00320 public static void FtpRemoveFile(string path, string username, string password)
00321 {
00322 try
00323 {
00324 FtpWebRequest request = (FtpWebRequest)WebRequest.Create(path);
00325 request.Method = WebRequestMethods.Ftp.DeleteFile;
00326 request.Credentials = new NetworkCredential(username, password);
00327
00328 using (FtpWebResponse removeResponse = (FtpWebResponse)request.GetResponse())
00329 {
00330 using (Stream removeResponseStream = removeResponse.GetResponseStream())
00331 using (StreamReader removeReader = new StreamReader(removeResponseStream))
00332 {
00333 string listDetails = removeReader.ReadToEnd();
00334 }
00335 }
00336 }
00337 catch (WebException e)
00338 {
00339 Console.WriteLine(e);
00340 throw;
00341 }
00342 }
00343
00344 public static bool FtpDirectoryExists(string location, string username, string password)
00345 {
00346 try
00347 {
00348 FtpWebRequest request = (FtpWebRequest)WebRequest.Create(location.TrimEnd('/'));
00349 request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
00350 request.Credentials = new NetworkCredential(username, password);
00351
00352 using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
00353 {
00354 using (Stream responseStream = response.GetResponseStream())
00355 using (StreamReader responseReader = new StreamReader(responseStream))
00356 {
00357 string listDetails = responseReader.ReadToEnd();
00358
00359 return !string.IsNullOrEmpty(listDetails);
00360 }
00361 }
00362 }
00363 catch (WebException exception)
00364 {
00365 if (exception.Message.Contains("550"))
00366 {
00367 return false;
00368 }
00369 throw;
00370 }
00371 }
00372
00373 public static bool FtpUploadFile(string targetLocation, string file, string username, string password)
00374 {
00375 FtpWebRequest request = (FtpWebRequest)WebRequest.Create(targetLocation.TrimEnd('/'));
00376 request.Method = WebRequestMethods.Ftp.UploadFile;
00377 request.Credentials = new NetworkCredential(username, password);
00378 request.UseBinary = true;
00379 request.Proxy = null;
00380
00381 FileInfo fileInfo = new FileInfo(file);
00382 byte[] buffer = new byte[fileInfo.Length];
00383
00384 using (FileStream fileStream = fileInfo.OpenRead())
00385 {
00386 fileStream.Read(buffer, 0, (int)fileInfo.Length);
00387 }
00388
00389 using (Stream writer = request.GetRequestStream())
00390 {
00391 writer.Write(buffer, 0, (int)fileInfo.Length);
00392 }
00393
00394 FtpWebResponse response = (FtpWebResponse)request.GetResponse();
00395
00396 return response.StatusCode == FtpStatusCode.FileActionOK;
00397
00398 }
00399
00400 public static void FtpCreateDirectory(string location, string username, string password)
00401 {
00402 FtpWebRequest request = (FtpWebRequest)WebRequest.Create(location.TrimEnd('/'));
00403 request.Method = WebRequestMethods.Ftp.MakeDirectory;
00404 request.Credentials = new NetworkCredential(username, password);
00405
00406 using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
00407 {
00408 using (Stream responseStream = response.GetResponseStream())
00409 using (StreamReader responseReader = new StreamReader(responseStream))
00410 {
00411 string listDetails = responseReader.ReadToEnd();
00412 }
00413 }
00414 }
00415
00416
00417 #endregion
00418
00419 #region Zip Wrappers
00420
00421 public static void ZipFiles(string tempDirectory, string outputPathAndFile)
00422 {
00423 if (File.Exists(outputPathAndFile))
00424 {
00425 try
00426 {
00427 File.Delete(outputPathAndFile);
00428 }
00429 catch (Exception) { }
00430
00431 }
00432
00433 FastZip fastZip = new FastZip();
00434 fastZip.CreateZip(outputPathAndFile, tempDirectory, true, string.Empty);
00435
00436
00437 }
00438
00439 #endregion
00440
00441 #region File and Directory Routines
00442
00443 public static void DeleteFile(string file)
00444 {
00445 try
00446 {
00447 if (!string.IsNullOrEmpty(file) && File.Exists(file))
00448 {
00449 File.Delete(file);
00450 }
00451 }
00452 catch { }
00453 }
00454
00455 public static string OpenDll(Form parent, string title)
00456 {
00457 using (OpenFileDialog openFileDialog = new OpenFileDialog())
00458 {
00459 openFileDialog.CheckFileExists = true;
00460 openFileDialog.CheckPathExists = true;
00461 openFileDialog.Title = title;
00462 openFileDialog.Filter = "DLL Assemblies (*.dll) | *.dll";
00463 openFileDialog.Multiselect = false;
00464
00465 if (openFileDialog.ShowDialog(parent) == DialogResult.Cancel)
00466 {
00467 return string.Empty;
00468 }
00469
00470 return openFileDialog.FileName;
00471
00472 }
00473 }
00474
00475 public static void DeleteDirectory(string directory, bool recurse)
00476 {
00477 if (!string.IsNullOrEmpty(directory) && Directory.Exists(directory))
00478 {
00479 try
00480 {
00481 Directory.Delete(directory, recurse);
00482 }
00483 catch (Exception) { }
00484
00485 }
00486 }
00487
00488 public static string CreateTempDirectory()
00489 {
00490 string tempDirectory = Path.GetTempFileName();
00491
00492 DeleteFile(tempDirectory);
00493
00494 tempDirectory = Path.ChangeExtension(tempDirectory, "");
00495
00496 Directory.CreateDirectory(tempDirectory);
00497 return tempDirectory;
00498 }
00499
00500 public static void CopyFiles(string sourceDirectory, string targetPath, string filter, bool removeIfExists)
00501 {
00502 string[] files = Directory.GetFiles(sourceDirectory, filter);
00503
00504 foreach (string file in files)
00505 {
00506 string targetFilePath = Path.Combine(targetPath, Path.GetFileName(file));
00507
00508 if (removeIfExists)
00509 {
00510 DeleteFile(targetFilePath);
00511 }
00512
00513 File.Copy(file, targetFilePath);
00514 }
00515 }
00516
00517 #endregion
00518
00519
00520 }
00521 }