public static void GetWorkshopsFile() { FileStream fs = null; StreamWriter sw = null; try { fs = new FileStream("workshopInfo.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite); sw = new StreamWriter(fs); List <Workshop> workshops = Data.GetWorkshops(); fs.SetLength(0); foreach (var workshop in workshops.Where(w => w.GetType().Name == nameof(OnlineWorkshop))) { OnlineWorkshop ws = (OnlineWorkshop)workshop; string teacher; if (ws.Presenter != null) { teacher = ws.Presenter.FirstName + " " + ws.Presenter.LastName; } else { teacher = "None"; } sw.WriteLine($"ONLINE(Title: {ws.Title}, Description: {ws.Description}, Teacher: {teacher}, Capacity: {ws.Capacity}, URL:{ws.URL})"); } foreach (var workshop in workshops.Where(w => w.GetType().Name == nameof(InBuildingWorkshop))) { InBuildingWorkshop ws = (InBuildingWorkshop)workshop; string teacher; if (ws.Presenter != null) { teacher = ws.Presenter.FirstName + " " + ws.Presenter.LastName; } else { teacher = "None"; } sw.WriteLine($"INBUILDING(Title:{ws.Title}, Description:{ws.Description}, Teacher:{teacher}, Capacity:{ws.Capacity}, Location: {ws.Address} {ws.RoomNumber}) "); //var path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); //path = path.Substring(6); } Process.Start("notepad.exe", "workshopInfo"); } catch (IOException) { MessageBox.Show("Error writing file"); } finally { if (sw != null) { sw.Close(); } } }
private void btnCreate_Click(object sender, EventArgs e) { //Get workshop data string title = tbxTitle.Text; string description = rtbDescription.Text; int capacity = (int)numCapacity.Value; //Create a new workshop try { Workshop ws = null; if (!String.IsNullOrEmpty(title) || !String.IsNullOrEmpty(description) || capacity > 0) { if (rbInBuilding.Checked) { string address = tbxAddress.Text; int roomNumber = (int)numRoomNumber.Value; if (String.IsNullOrEmpty(address) || roomNumber <= 0) { throw new ArgumentException("Please provide valid address and room number values"); } ws = new InBuildingWorkshop(title, description, capacity, address, roomNumber); } else if (rbOnline.Checked) { string url = tbxURL.Text; if (String.IsNullOrEmpty(url)) { throw new ArgumentException("URL cannot be null or empty"); } ws = new OnlineWorkshop(title, description, capacity, url); } else { MessageBox.Show($"Please select a workshop type (Online/In Building)"); return; } //Add to list this.workshops.Add(ws); Data.UpdateWorkshops(this.workshops); Dashboard dashboard = new Dashboard(); this.Hide(); dashboard.ShowDialog(); } else { MessageBox.Show($"Please fill all the required fields."); return; } } catch (Exception ex) { MessageBox.Show(ex.Message); } }