public static ProjectItem FromFile(String filePath, DirectoryItem parent) { try { if (!File.Exists(filePath)) { throw new Exception("File does not exist: " + filePath); } using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { Type type = null; switch ((new FileInfo(filePath).Extension).ToLower()) { case ScriptItem.Extension: type = typeof(ScriptItem); break; case PointerItem.Extension: type = typeof(PointerItem); break; case InstructionItem.Extension: type = typeof(InstructionItem); break; case DotNetItem.Extension: type = typeof(DotNetItem); break; case JavaItem.Extension: type = typeof(JavaItem); break; default: return(null); } DataContractJsonSerializer serializer = new DataContractJsonSerializer(type); ProjectItem projectItem = serializer.ReadObject(fileStream) as ProjectItem; projectItem.name = Path.GetFileNameWithoutExtension(filePath); projectItem.Parent = parent; projectItem.IsFileAssociated = true; return(projectItem); } } catch (Exception ex) { Logger.Log(LogLevel.Error, "Error loading file", ex); throw ex; } }
/// <summary> /// Gets the list of files in the directory Name passed. /// </summary> /// <returns>Returns the List of File info for this directory. /// Return null if an exception is raised.</returns> public FullyObservableCollection <ProjectItem> BuildChildren() { FullyObservableCollection <ProjectItem> projectItems = new FullyObservableCollection <ProjectItem>(); try { IEnumerable <DirectoryInfo> subdirectories = Directory.GetDirectories(this.FullPath).Select(subdirectory => new DirectoryInfo(subdirectory)); foreach (DirectoryInfo subdirectory in subdirectories) { try { projectItems.Add(DirectoryItem.FromDirectory(subdirectory.FullName)); } catch (Exception ex) { Logger.Log(LogLevel.Error, "Error loading directory", ex); } } } catch (Exception ex) { Logger.Log(LogLevel.Error, "Error fetching directories", ex); } try { foreach (FileInfo file in Directory.GetFiles(this.FullPath).Select(directoryFile => new FileInfo(directoryFile))) { try { ProjectItem projectItem = ProjectItem.FromFile(file.FullName, this); if (projectItem != null) { projectItems.Add(projectItem); } } catch (Exception ex) { Logger.Log(LogLevel.Error, "Error reading project item", ex); } } } catch (Exception ex) { Logger.Log(LogLevel.Error, "Error fetching files", ex); } return(projectItems); }