2
0

FilePath.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading;
  5. namespace SharpCifs.Util.Sharpen
  6. {
  7. public class FilePath
  8. {
  9. private string _path;
  10. private static long _tempCounter;
  11. public FilePath()
  12. {
  13. }
  14. public FilePath(string path)
  15. : this((string)null, path)
  16. {
  17. }
  18. public FilePath(FilePath other, string child)
  19. : this((string)other, child)
  20. {
  21. }
  22. public FilePath(string other, string child)
  23. {
  24. if (other == null)
  25. {
  26. _path = child;
  27. }
  28. else
  29. {
  30. while (!string.IsNullOrEmpty(child)
  31. && (child[0] == Path.DirectorySeparatorChar
  32. || child[0] == Path.AltDirectorySeparatorChar))
  33. child = child.Substring(1);
  34. if (!string.IsNullOrEmpty(other)
  35. && other[other.Length - 1] == Path.VolumeSeparatorChar)
  36. other += Path.DirectorySeparatorChar;
  37. _path = Path.Combine(other, child);
  38. }
  39. }
  40. public static implicit operator FilePath(string name)
  41. {
  42. return new FilePath(name);
  43. }
  44. public static implicit operator string(FilePath filePath)
  45. {
  46. return filePath == null ? null : filePath._path;
  47. }
  48. public override bool Equals(object obj)
  49. {
  50. FilePath other = obj as FilePath;
  51. if (other == null)
  52. return false;
  53. return GetCanonicalPath() == other.GetCanonicalPath();
  54. }
  55. public override int GetHashCode()
  56. {
  57. return _path.GetHashCode();
  58. }
  59. public bool CreateNewFile()
  60. {
  61. try
  62. {
  63. //Stream.`Close` method deleted
  64. //File.Open (_path, FileMode.CreateNew).Close ();
  65. File.Open(_path, FileMode.CreateNew).Dispose();
  66. return true;
  67. }
  68. catch
  69. {
  70. return false;
  71. }
  72. }
  73. public static FilePath CreateTempFile()
  74. {
  75. return new FilePath(Path.GetTempFileName());
  76. }
  77. public static FilePath CreateTempFile(string prefix, string suffix)
  78. {
  79. return CreateTempFile(prefix, suffix, null);
  80. }
  81. public static FilePath CreateTempFile(string prefix, string suffix, FilePath directory)
  82. {
  83. string file;
  84. if (prefix == null)
  85. {
  86. throw new ArgumentNullException("prefix");
  87. }
  88. if (prefix.Length < 3)
  89. {
  90. throw new ArgumentException("prefix must have at least 3 characters");
  91. }
  92. string str = (directory == null) ? Path.GetTempPath() : directory.GetPath();
  93. do
  94. {
  95. file = Path.Combine(str, prefix + Interlocked.Increment(ref _tempCounter) + suffix);
  96. } while (File.Exists(file));
  97. new FileOutputStream(file).Close();
  98. return new FilePath(file);
  99. }
  100. public void DeleteOnExit()
  101. {
  102. }
  103. public FilePath GetAbsoluteFile()
  104. {
  105. return new FilePath(Path.GetFullPath(_path));
  106. }
  107. public string GetAbsolutePath()
  108. {
  109. return Path.GetFullPath(_path);
  110. }
  111. public FilePath GetCanonicalFile()
  112. {
  113. return new FilePath(GetCanonicalPath());
  114. }
  115. public string GetCanonicalPath()
  116. {
  117. return Path.GetFullPath(_path);
  118. }
  119. public string GetName()
  120. {
  121. return Path.GetFileName(_path);
  122. }
  123. public FilePath GetParentFile()
  124. {
  125. return new FilePath(Path.GetDirectoryName(_path));
  126. }
  127. public string GetPath()
  128. {
  129. return _path;
  130. }
  131. public bool IsAbsolute()
  132. {
  133. return Path.IsPathRooted(_path);
  134. }
  135. public bool IsDirectory()
  136. {
  137. return false; // FileHelper.Instance.IsDirectory(this);
  138. }
  139. public bool IsFile()
  140. {
  141. return false; //FileHelper.Instance.IsFile (this);
  142. }
  143. public long LastModified()
  144. {
  145. return 0; // FileHelper.Instance.LastModified(this);
  146. }
  147. public long Length()
  148. {
  149. return 0; // FileHelper.Instance.Length(this);
  150. }
  151. public string[] List()
  152. {
  153. return List(null);
  154. }
  155. public string[] List(IFilenameFilter filter)
  156. {
  157. try
  158. {
  159. if (IsFile())
  160. return null;
  161. List<string> list = new List<string>();
  162. foreach (string filePth in Directory.GetFileSystemEntries(_path))
  163. {
  164. string fileName = Path.GetFileName(filePth);
  165. if ((filter == null) || filter.Accept(this, fileName))
  166. {
  167. list.Add(fileName);
  168. }
  169. }
  170. return list.ToArray();
  171. }
  172. catch
  173. {
  174. return null;
  175. }
  176. }
  177. public FilePath[] ListFiles()
  178. {
  179. try
  180. {
  181. if (IsFile())
  182. return null;
  183. List<FilePath> list = new List<FilePath>();
  184. foreach (string filePath in Directory.GetFileSystemEntries(_path))
  185. {
  186. list.Add(new FilePath(filePath));
  187. }
  188. return list.ToArray();
  189. }
  190. catch
  191. {
  192. return null;
  193. }
  194. }
  195. static void MakeDirWritable(string dir)
  196. {
  197. //FileHelper.Instance.MakeDirWritable (dir);
  198. }
  199. static void MakeFileWritable(string file)
  200. {
  201. //FileHelper.Instance.MakeFileWritable (file);
  202. }
  203. public bool Mkdir()
  204. {
  205. try
  206. {
  207. if (Directory.Exists(_path))
  208. return false;
  209. Directory.CreateDirectory(_path);
  210. return true;
  211. }
  212. catch (Exception)
  213. {
  214. return false;
  215. }
  216. }
  217. public bool Mkdirs()
  218. {
  219. try
  220. {
  221. if (Directory.Exists(_path))
  222. return false;
  223. Directory.CreateDirectory(_path);
  224. return true;
  225. }
  226. catch
  227. {
  228. return false;
  229. }
  230. }
  231. public bool RenameTo(FilePath file)
  232. {
  233. return RenameTo(file._path);
  234. }
  235. public bool RenameTo(string name)
  236. {
  237. return false; // FileHelper.Instance.RenameTo(this, name);
  238. }
  239. public bool SetLastModified(long milis)
  240. {
  241. return false; // FileHelper.Instance.SetLastModified(this, milis);
  242. }
  243. public bool SetReadOnly()
  244. {
  245. return false; // FileHelper.Instance.SetReadOnly(this);
  246. }
  247. public Uri ToUri()
  248. {
  249. return new Uri(_path);
  250. }
  251. // Don't change the case of this method, since ngit does reflection on it
  252. public bool CanExecute()
  253. {
  254. return false; // FileHelper.Instance.CanExecute(this);
  255. }
  256. // Don't change the case of this method, since ngit does reflection on it
  257. public bool SetExecutable(bool exec)
  258. {
  259. return false; // FileHelper.Instance.SetExecutable(this, exec);
  260. }
  261. public string GetParent()
  262. {
  263. string p = Path.GetDirectoryName(_path);
  264. if (string.IsNullOrEmpty(p) || p == _path)
  265. return null;
  266. return p;
  267. }
  268. public override string ToString()
  269. {
  270. return _path;
  271. }
  272. static internal string PathSeparator
  273. {
  274. get { return Path.PathSeparator.ToString(); }
  275. }
  276. static internal char PathSeparatorChar
  277. {
  278. get { return Path.PathSeparator; }
  279. }
  280. static internal char SeparatorChar
  281. {
  282. get { return Path.DirectorySeparatorChar; }
  283. }
  284. static internal string Separator
  285. {
  286. get { return Path.DirectorySeparatorChar.ToString(); }
  287. }
  288. }
  289. }