PageRenderTime 35ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/Development/Yaaf.Wp7.NativeAccess/IO/Internals/HTC/HtcFileSystemAccess.cs

#
C# | 310 lines | 243 code | 48 blank | 19 comment | 29 complexity | e7303263dd780d97d39a2b45f36bb72b MD5 | raw file
  1. namespace Yaaf.WP7.NativeAccess.IO.Internals.HTC
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.IO.IsolatedStorage;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. using Yaaf.WP7.NativeAccess.ComWrapper.HTC.COM_FileRw;
  11. using Yaaf.WP7.NativeAccess.ComWrapper.HTC.COM_NativeAccess;
  12. using Yaaf.WP7.NativeAccess.Helper;
  13. internal class HtcFileSystemAccess : IFileSystemAccess
  14. {
  15. private HTCFileAccess access;
  16. public HtcFileSystemAccess()
  17. {
  18. access = new HTCFileAccess();
  19. }
  20. public void ExecuteFile(string fullName)
  21. {
  22. var extension = Path.GetExtension(fullName);
  23. var value =
  24. Yaaf.WP7.NativeAccess.Registry.Registry.GetValue(
  25. WP7.NativeAccess.Registry.Registry.HkeyClassesRoot + "\\" + extension + "\\");
  26. var stringValue = value == null ? null : value.ToString();
  27. //RegistryKey key = Registry.ClassesRoot.OpenSubKey(extension);
  28. //string stringValue = null;
  29. //string str2 = null;
  30. //if (key != null)
  31. //{
  32. // stringValue = key.GetStringValue();
  33. // key.Close();
  34. //}
  35. if (string.IsNullOrEmpty(stringValue))
  36. {
  37. throw new InvalidOperationException("No program is associated with this extension");
  38. }
  39. //RegistryKey key2 = Registry.ClassesRoot.OpenSubKey(stringValue + @"\Shell\Open\Command");
  40. //string str2;
  41. //if (key2 != null)
  42. //{
  43. // str2 = key2.GetStringValue();
  44. // key2.Close();
  45. //}
  46. var value2 = WP7.NativeAccess.Registry.Registry.GetValue(
  47. WP7.NativeAccess.Registry.Registry.HkeyClassesRoot + "\\" + stringValue + @"\Shell\Open\Command\");
  48. var stringValue2 = value2 == null ? null : value2.ToString();
  49. if (string.IsNullOrEmpty(stringValue2))
  50. {
  51. throw new InvalidOperationException("No program is associated with this extension");
  52. }
  53. Shell.LaunchSessionByUri(stringValue2.Replace("%s", fullName));
  54. }
  55. public Stream OpenFile(string path, FileMode mode, System.IO.FileAccess access)
  56. {
  57. System.IO.IsolatedStorage.IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
  58. file.CreateDirectory(DeviceInfo.TempDir);
  59. var fullTempDir = PathInfo.ToGeneral(DeviceInfo.TempDir);
  60. var fileName = System.IO.Path.GetFileName(path);
  61. var fullTempFileName = System.IO.Path.Combine(fullTempDir, fileName);
  62. var tempStoragePath = System.IO.Path.Combine(DeviceInfo.TempDir, fileName);
  63. var pathInfo = GetPathInformation(path);
  64. if (pathInfo == PathInformation.File)
  65. {
  66. CopyFile(path, fullTempFileName, true);
  67. }
  68. else
  69. {
  70. IsolatedStorageFile isolated = IsolatedStorageFile.GetUserStoreForApplication();
  71. if (isolated.FileExists(tempStoragePath))
  72. {
  73. isolated.DeleteFile(tempStoragePath);
  74. }
  75. }
  76. return new StreamWrapper(file.OpenFile(tempStoragePath, mode, access), path, fullTempFileName);
  77. }
  78. private static Exception GetException(uint errorCode)
  79. {
  80. switch (errorCode)
  81. {
  82. case 2:
  83. throw new FileNotFoundException("source not found!");
  84. case 4:
  85. return new IOException("Could not open file");
  86. case 5:
  87. return new UnauthorizedAccessException("Access is denied.");
  88. case 16:
  89. return new IOException("Unable to delete the directory. ");
  90. case 3:
  91. return new DirectoryNotFoundException("Directory not found");
  92. case 18:
  93. return new FileNotFoundException("file not found!");
  94. case 32:
  95. return new IOException(
  96. "The process can not access the file because it is used by another process. ");
  97. case 80:
  98. return new IOException("The file exists.");
  99. }
  100. return new IOException("Unknown IO Error: " + errorCode);
  101. }
  102. private static void CheckError(uint errorCode)
  103. {
  104. if (errorCode == 0)
  105. {
  106. return;
  107. }
  108. var error = GetException(errorCode);
  109. error.Data["ErrorCode"] = errorCode;
  110. throw error;
  111. }
  112. public void CopyFile( string source, string target, bool overwrite = true)
  113. {
  114. CheckError(access.CopyFile(source, target, (uint)(overwrite ? 1 : 0)));
  115. }
  116. public void MoveFile(string path, string dest, bool overwrite)
  117. {
  118. CopyFile(path, dest, overwrite);
  119. DeleteFile(path);
  120. //var op = new Helper.ProvisionXmlFileOperations();
  121. //op.AddMoveOperation(path, dest, ProvisionXmlFileAttributes.Hidden | ProvisionXmlFileAttributes.ReadOnly | ProvisionXmlFileAttributes.System);
  122. //ProvisionXml.Provision(op.ToString());
  123. }
  124. public void DeleteFile(string target)
  125. {
  126. CheckError(access.DeleteFile(target));
  127. }
  128. public void DeleteDirectory(string fullName, bool recursive)
  129. {
  130. if (recursive)
  131. {
  132. DeleteRecursive(fullName);
  133. }
  134. else
  135. {
  136. DeleteEmptyFolder(fullName);
  137. }
  138. }
  139. private void DeleteRecursive(string fullName)
  140. {
  141. foreach (var subItem in EnumerateFileSystemEntries(fullName, true, true, null, true))
  142. {
  143. var itemPath = Path.Combine(fullName, subItem.Name);
  144. bool isFile = subItem.IsFile.HasValue ? subItem.IsFile.Value : ExistsFile(itemPath);
  145. if (isFile)
  146. {
  147. DeleteFile(itemPath);
  148. }
  149. else
  150. {
  151. DeleteRecursive(itemPath);
  152. }
  153. }
  154. DeleteEmptyFolder(fullName);
  155. }
  156. private void DeleteEmptyFolder(string fullName)
  157. {
  158. var op = new ProvisionXmlFileOperations();
  159. op.AddDeleteDirectoryOperation(fullName);
  160. try
  161. {
  162. Provision.ProvisionXml.Provision(op.ToString());
  163. }
  164. catch (IOException)
  165. {
  166. if (ExistsDirectory(fullName))
  167. {
  168. throw;
  169. }
  170. }
  171. if (ExistsDirectory(fullName))
  172. {
  173. throw new IOException("Directory could not be deleted (maybe is is not empty)!");
  174. }
  175. }
  176. public void CreateDirectory(string fullName)
  177. {
  178. Helper.ProvisionXmlFileOperations op = new ProvisionXmlFileOperations();
  179. op.AddMakeDirOperation(fullName);
  180. Provision.ProvisionXml.Provision(op.ToString());
  181. // Give it some time to execute
  182. Thread.Sleep(500);
  183. if (!ExistsDirectory(fullName))
  184. {
  185. throw new IOException("Directory could not be created!");
  186. }
  187. }
  188. public bool ExistsFile(string fullName)
  189. {
  190. return GetPathInformation(fullName) == PathInformation.File;
  191. }
  192. public bool ExistsDirectory(string fullName)
  193. {
  194. return GetPathInformation(fullName) == PathInformation.Directory;
  195. }
  196. public PathInformation GetPathInformation(string fullName)
  197. {
  198. uint info;
  199. var errorCode = access.GetInformation(fullName, out info);
  200. if (errorCode == 18 || errorCode == 3)
  201. {
  202. return PathInformation.NotExistent;
  203. }
  204. CheckError(errorCode);
  205. switch (info)
  206. {
  207. case 0:
  208. return PathInformation.NotExistent;
  209. case 1:
  210. return PathInformation.File;
  211. case 2:
  212. return PathInformation.Directory;
  213. default:
  214. throw new ArgumentOutOfRangeException("fullName", "Unknown information: " + info);
  215. }
  216. }
  217. private IEnumerable<string> EnumerateEntries(string directory, string search, bool fullPath = true)
  218. {
  219. uint num;
  220. string strPath = directory.EndsWith(@"\") ? directory : (directory + @"\");
  221. string strSearch = string.IsNullOrEmpty(search) ? "*" : search;
  222. access.GetAllFilesBufferSize(strPath, strSearch, out num);
  223. if (num > 0)
  224. {
  225. byte[] byteBuffer = new byte[num];
  226. access.GetAllFileNames(strPath, strSearch, byteBuffer, num);
  227. char[] trimChars = new char[2];
  228. trimChars[1] = '\\';
  229. foreach (string str3 in Encoding.Unicode.GetString(byteBuffer, 0, byteBuffer.Length).TrimEnd(trimChars).Split(new char[] { '\\' }))
  230. {
  231. yield return fullPath ? (strPath + str3) : str3;
  232. }
  233. }
  234. }
  235. public IEnumerable<FileSystemEntryStruct> EnumerateFileSystemEntries(string directory, bool includeFiles, bool includeDirectories, string search, bool fullPath = true)
  236. {
  237. if (includeDirectories && includeFiles)
  238. {
  239. return EnumerateEntries(directory, search, fullPath).Select(s => new FileSystemEntryStruct(s));
  240. }
  241. if (includeDirectories)
  242. {
  243. return
  244. EnumerateEntries(directory, search, fullPath).Where(
  245. s => GetPathInformation(s) == PathInformation.Directory).Select(s => new FileSystemEntryStruct(s, false));
  246. }
  247. if (includeFiles)
  248. {
  249. return
  250. EnumerateEntries(directory, search, fullPath).Where(
  251. s => GetPathInformation(s) == PathInformation.File).Select(s => new FileSystemEntryStruct(s, true));
  252. }
  253. return new FileSystemEntryStruct[0];
  254. }
  255. public long GetFileSize(string fullName)
  256. {
  257. uint size;
  258. access.GetFileSize(fullName, out size);
  259. return size;
  260. }
  261. }
  262. }