PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/DBCopyTool/Class/cHelper.cs

#
C# | 102 lines | 88 code | 11 blank | 3 comment | 6 complexity | b68bfed3a46ed4acde052789e27fcce0 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Data.Sql;
  7. using System.Data;
  8. using System.Net;
  9. using System.IO;
  10. namespace DatabaseCopier.Class
  11. {
  12. static class cHelper
  13. {
  14. public static DataTable GetServersFromNetwork()
  15. {
  16. // Retrieve the enumerator instance and then the data.
  17. SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
  18. DataTable dtInstances = instance.GetDataSources();
  19. return dtInstances;
  20. }
  21. public static bool CheckFilePathRegex(string sFilepath)
  22. {
  23. //check if dirs are dirs
  24. Regex reFoldername = new Regex("^[A-Za-z]:\\\\(.)*(\\\\)*$");
  25. if (!reFoldername.IsMatch(sFilepath) || !sFilepath.EndsWith("\\"))
  26. {
  27. return false;
  28. }
  29. else
  30. {
  31. return true;
  32. }
  33. }
  34. public static string MakeUncPath(string sText, string sServer)
  35. {
  36. string sUncPath;
  37. char driveLetter;
  38. driveLetter = sText.Substring(0, 1)[0];
  39. sServer = sServer.Replace(sServer, getIpFromServer(sServer));
  40. sUncPath = sText.Replace(driveLetter + ":\\", "\\\\" + sServer + "\\" + driveLetter + "$\\");
  41. return sUncPath;
  42. }
  43. public static void DeleteFileIfExists(string sUncFileName)
  44. {
  45. //Check if file exists and delete..
  46. if (File.Exists(sUncFileName))
  47. {
  48. File.Delete(sUncFileName);
  49. }
  50. }
  51. private static string getIpFromServer(string sServername)
  52. {
  53. string returnIP;
  54. if (sServername.Contains(@"\"))
  55. {
  56. sServername = sServername.Substring(0, sServername.IndexOf(@"\"));
  57. IPHostEntry ip = Dns.GetHostEntry(sServername);
  58. IPAddress[] IpA = ip.AddressList;
  59. if (IpA[0].AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
  60. {
  61. returnIP = IpA[0].ToString().Replace(":", "-").Replace("%", "s") + ".ipv6-literal.net";
  62. }
  63. else
  64. {
  65. returnIP = IpA[0].ToString();
  66. }
  67. }
  68. else
  69. {
  70. //To get a host with a www address
  71. IPHostEntry ip = Dns.GetHostEntry(sServername);
  72. IPAddress[] IpA = ip.AddressList;
  73. if (IpA[0].AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
  74. {
  75. returnIP = IpA[0].ToString().Replace(":", "-").Replace("%", "s") + ".ipv6-literal.net";
  76. }
  77. else
  78. {
  79. returnIP = IpA[0].ToString();
  80. }
  81. }
  82. return returnIP;
  83. }
  84. }
  85. }