public ProjectModel OpenProject(string filePath) { if (File.Exists(filePath)) { var projectModel = new ProjectModelZip(m_projectManager); projectModel.ProjectFilePath = filePath; BitmapImage image = null; ObstacleDataModel obstacleData = null; using (Package package = ZipPackage.Open(projectModel.ProjectFilePath, FileMode.Open)) { if (package.PartExists(IMAGE_URI)) { PackagePart imagePackagePart = package.GetPart(IMAGE_URI); image = new BitmapImage(); image.BeginInit(); image.CacheOption = BitmapCacheOption.Default; image.StreamSource = imagePackagePart.GetStream(FileMode.Open, FileAccess.Read); image.EndInit(); } if (package.PartExists(OBSTACLE_DATA_URI)) { PackagePart obstacleDataPart = package.GetPart(OBSTACLE_DATA_URI); XmlSerializer ser = new XmlSerializer(typeof(ObstacleDataModel)); obstacleData = (ObstacleDataModel)ser.Deserialize(obstacleDataPart.GetStream()); } } if (image != null || obstacleData != null) { projectModel.RaceTrackModel = new RaceTrackModel(); projectModel.RaceTrackModel.RaceTrackImage = image; projectModel.RaceTrackModel.Obstacles = obstacleData; } return(projectModel); } else { throw new IOException(string.Format("Project File: {0} not found.", filePath)); } }
public ProjectModel CreateNewProject(NewProjectInfoModel projectInformation) { string filePath = projectInformation.FilePath; bool canCreateNew = !File.Exists(filePath); string projectName = Path.GetFileNameWithoutExtension(filePath); if (!canCreateNew) { var userInput = MessageBox.Show( string.Format("Project: {0} already exists in selected location. Do you wish to replace it?", projectName), "Problem with Project Creation", MessageBoxButton.YesNo, MessageBoxImage.Question); if (userInput == MessageBoxResult.Yes) { while (File.Exists(filePath)) { bool forgetIt = false; try { File.Delete(filePath); // Give the system some time to get rid of the file // Side Note: On a slow system or a system underload, // this might not be enough time. Thread.Sleep(5); } catch (Exception ex) { userInput = MessageBox.Show( "There was a problem getting rid of the old project file. Do you wish to try again?", "Deletion Problem", MessageBoxButton.YesNo, MessageBoxImage.Error); forgetIt = userInput == MessageBoxResult.No; } if (forgetIt) { return(null); } } } else { return(null); } } var defaultLayer = new ObstacleLayerModel() { LayerName = "Layer 1" }; var obstacleData = new ObstacleDataModel(); obstacleData.AddLayer(defaultLayer); var raceTrackModel = new RaceTrackModel() { RaceTrackImage = null, Obstacles = obstacleData }; var projectModel = new ProjectModelZip(m_projectManager) { ProjectFilePath = filePath, RaceTrackModel = raceTrackModel }; SaveProject(projectModel); return(projectModel); }