/// <summary> /// Displays the picture of the given Executive, or blank if picture not found /// </summary> /// <param name="exec"> /// The Executive to display their picture /// </param> private void SetPicture(Executive exec) { // The expected full path of the picture file string imageFile = this.serverDir + "/" + exec.imageUrl; if (exec.imageUrl == "" || exec.imageUrl == null || exec.imageUrl.Length < 1) { // If the Executive is missing image file information this.textBoxStatus.Text = "Executive is missing image url"; this.pictureBox1.Image = null; } else if (!File.Exists(imageFile)) { // If the picture file could not be found - either doesn't exist or can't access server this.textBoxStatus.Text = "Could not find picture: " + imageFile; this.pictureBox1.Image = null; } else { // Read the picture file and update the picture box to display it this.pictureBox1.Image = Image.FromStream(new MemoryStream(File.ReadAllBytes(imageFile))); } }
/// <summary> /// Loads the Executive json info file, and initializes the current list of Executives based on the file /// </summary> private void LoadJson() { this.Executives = new List <Executive>(); this.ExecutiveNames = new List <ExecutivePosition>(); StreamReader r = null; try { r = new StreamReader(this.JsonPath); string json = r.ReadToEnd(); // Displays the json file in a textbox this.textBoxDebug.Text = json; JsonTextReader reader = new JsonTextReader(new StringReader(json)); while (reader.Read()) { // Token is '{' representing an object if (reader.TokenType == JsonToken.StartObject) { var exec = new Executive(); // Loop through all properties of current object while (reader.Read() && reader.TokenType == JsonToken.PropertyName) { // Check the name of the property, and initialize the values of the Executive accordingly switch ((String)reader.Value) { case "position": if (reader.Read() && reader.TokenType == JsonToken.String) { exec.position = reader.Value.ToString(); this.ExecutiveNames.Add(new ExecutivePosition(exec.position)); } break; case "fullName": if (reader.Read() && reader.TokenType == JsonToken.String) { exec.fullName = reader.Value.ToString(); } break; case "email": if (reader.Read() && reader.TokenType == JsonToken.String) { exec.email = reader.Value.ToString(); } break; case "bio": if (reader.Read() && reader.TokenType == JsonToken.String) { exec.bio = reader.Value.ToString(); } break; case "imageUrl": if (reader.Read() && reader.TokenType == JsonToken.String) { exec.imageUrl = reader.Value.ToString(); } break; default: // Found an irrelevant property, try to loop again to check next property reader.Read(); break; } } // Finished reading one Executive object this.Executives.Add(exec); } } // Successfully loaded the json file this.tempString = "Loaded file: " + this.JsonPath; } catch (Exception e) { // Could not load the json file, most likely couldn't find it on the server this.tempString = "Could not find file: " + this.JsonPath; } finally { if (r != null) { r.Close(); } } }