userConfig.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace CassiniDev
  6. {
  7. public class userConfig
  8. {
  9. public userConfig() {
  10. }
  11. static int _WebPort = 0;
  12. /// <summary>
  13. /// 端口
  14. /// </summary>
  15. public static int WebPort
  16. {
  17. get { return _WebPort; }
  18. set { _WebPort = value; }
  19. }
  20. public static void LoadWebPort()
  21. {
  22. try
  23. {
  24. if (System.IO.File.Exists(configPath0))
  25. {
  26. _WebPort = Convert.ToInt32(System.IO.File.ReadAllText(configPath0, Encoding.UTF8));
  27. }
  28. else {
  29. if (System.IO.File.Exists(configPath))
  30. {
  31. _WebPort = Convert.ToInt32(System.IO.File.ReadAllText(configPath, Encoding.UTF8));
  32. }
  33. }
  34. }
  35. catch {
  36. _WebPort = 0;
  37. }
  38. }
  39. public static void SaveWebPort(int port)
  40. {
  41. try
  42. {
  43. _WebPort = port;
  44. System.IO.File.WriteAllText(configPath,port.ToString(),Encoding.UTF8);
  45. }
  46. catch { }
  47. try
  48. {
  49. System.IO.File.WriteAllText(configPath0, port.ToString(), Encoding.UTF8);
  50. }
  51. catch { }
  52. }
  53. static string configPath0 = BasePath + "Bin\\AspNet40.ini";
  54. static string configPath = BasePath + "userConfig.txt";
  55. /// <summary>
  56. /// WebForm和WinForm通用的取当前根目录的方法
  57. /// </summary>
  58. public static string BasePath
  59. {
  60. get
  61. {
  62. System.Diagnostics.Process p = System.Diagnostics.Process.GetCurrentProcess();
  63. //WebDev.WebServer visual studio web server
  64. //xxx.vhost Winform
  65. //w3wp IIS7
  66. //aspnet_wp IIS6
  67. string processName = p.ProcessName.ToLower();
  68. string retPath = System.Windows.Forms.Application.StartupPath;
  69. if (processName == "aspnet_wp" || processName == "w3wp" || processName == "webdev.webserver" || processName == "iisexpress")
  70. {
  71. if (System.Web.HttpContext.Current != null)
  72. retPath = System.Web.HttpContext.Current.Server.MapPath("~/").TrimEnd(new char[] { '\\' });
  73. else //当控件在定时器的触发程序中使用时就为空
  74. {
  75. retPath = System.AppDomain.CurrentDomain.BaseDirectory.TrimEnd(new char[] { '\\' });
  76. }
  77. }
  78. return GetFullDirectoryPath(retPath);
  79. }
  80. }
  81. /// <summary>
  82. /// 获取完整路径 只能为文件夹目录路径 检查路径结尾是否存在 “\”符号 如果不存在添加上
  83. /// </summary>
  84. /// <param name="directoryPath"></param>
  85. /// <returns></returns>
  86. public static string GetFullDirectoryPath(string directoryPath)
  87. {
  88. if (directoryPath.LastIndexOf("\\") != directoryPath.Length - 1)
  89. {
  90. directoryPath = directoryPath + "\\";
  91. }
  92. return directoryPath;
  93. }
  94. }
  95. }