Windows.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //------------------------------------------------------------------------------
  2. //
  3. // System.Private.Windows.cs
  4. //
  5. // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
  6. //
  7. // Author: Jim Richardson, [email protected]
  8. // Created: Tuesday, August 21, 2001
  9. //
  10. //------------------------------------------------------------------------------
  11. using System;
  12. using System.IO;
  13. using System.Diagnostics;
  14. using System.Text;
  15. using System.Runtime.InteropServices;
  16. namespace System
  17. {
  18. public sealed class PlatformSpecific
  19. {
  20. // TODO: Get more complete error code list
  21. public const int ERROR_FILE_NOT_FOUND = 2;
  22. public const int ERROR_PATH_NOT_FOUND = 3;
  23. public const int ERROR_TOO_MANY_OPEN_FILES = 4;
  24. public const int ERROR_ACCESS_DENIED = 5;
  25. public const int ERROR_INVALID_DRIVE = 15;
  26. public const int MAX_COMPUTERNAME_LENGTH = 31;
  27. public const int MAX_PATH = 260;
  28. public static readonly char AltDirectorySeparatorChar = '/'; // TODO: verify this
  29. public static readonly char DirectorySeparatorChar = '\\';
  30. public static readonly char[] InvalidPathChars = { '\\', '/', ':', '*', '?', '\"', '<', '>', '|' };
  31. public static readonly char PathSeparator = ';'; // might be a space for unix/linux
  32. public static readonly char VolumeSeparatorChar = ':';
  33. /// <summary>
  34. /// Gets the standard new line value
  35. /// </summary>
  36. public static string NewLine
  37. {
  38. get
  39. {
  40. return "\r\n";
  41. }
  42. }
  43. // TODO: verify the "W" versions are available on 95/98/ME
  44. // or whatever other windoze platforms we'll target
  45. [ DllImport("kernel32", EntryPoint="GetCommandLineW") ]
  46. public unsafe static extern string getCommandLine();
  47. [DllImport("kernel32", SetLastError=true)]
  48. private unsafe static extern int GetCurrentDirectory(int bufSize, StringBuilder buf);
  49. [DllImport("kernel32", SetLastError=true)]
  50. private unsafe static extern bool SetCurrentDirectory(string name);
  51. [DllImport("kernel32")]
  52. private unsafe static extern int GetLastError();
  53. [DllImport("libmono")]
  54. private unsafe static extern bool os_version(ref int platform, ref IntPtr version);
  55. public static OperatingSystem getOSVersion()
  56. {
  57. int platform = 0;
  58. IntPtr version = new IntPtr();
  59. if(os_version(ref platform, ref version))
  60. {
  61. string str = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(version);
  62. System.Runtime.InteropServices.Marshal.FreeBSTR(version);
  63. return new OperatingSystem((PlatformID)platform, new Version(str));
  64. }
  65. return null;
  66. }
  67. [DllImport("kernel32", SetLastError=true)]
  68. private unsafe static extern bool GetComputerName(StringBuilder buf, ref int bufSize);
  69. public static string getMachineName()
  70. {
  71. int capacity = MAX_COMPUTERNAME_LENGTH + 1;
  72. StringBuilder buf = new StringBuilder(capacity);
  73. if(!GetComputerName(buf, ref capacity))
  74. {
  75. Debug.Assert(false);
  76. // TODO: Determine if we should return error and if so
  77. // what error to return.
  78. }
  79. return buf.ToString();
  80. }
  81. public static string getCurrentDirectory()
  82. {
  83. int capacity = MAX_PATH;
  84. StringBuilder buf = new StringBuilder(capacity);
  85. int needed = GetCurrentDirectory(capacity, buf);
  86. if(needed > capacity)
  87. {
  88. capacity = needed;
  89. buf.Capacity = needed;
  90. needed = GetCurrentDirectory(capacity, buf);
  91. }
  92. if(needed > capacity || needed == 0)
  93. {
  94. Debug.Assert(false);
  95. return String.Empty;
  96. }
  97. return buf.ToString();
  98. }
  99. public static void setCurrentDirectory(string path)
  100. {
  101. if(!SetCurrentDirectory(path))
  102. {
  103. bool bNotFound;
  104. switch(GetLastError())
  105. { // TODO: Get more complete error code list
  106. case ERROR_FILE_NOT_FOUND:
  107. case ERROR_PATH_NOT_FOUND:
  108. case ERROR_TOO_MANY_OPEN_FILES:
  109. case ERROR_INVALID_DRIVE:
  110. bNotFound = true;
  111. break;
  112. default:
  113. bNotFound = false;
  114. break;
  115. }
  116. if(bNotFound)
  117. {
  118. throw new DirectoryNotFoundException();
  119. }
  120. throw new IOException();
  121. }
  122. }
  123. }
  124. }