/// <summary> /// Save a vCard file /// </summary> /// <param name="sender">The sender of the event</param> /// <param name="e">The event arguments</param> private void miSave_Click(object sender, EventArgs e) { using (SaveFileDialog dlg = new SaveFileDialog()) { dlg.Title = "Save vCard File"; dlg.DefaultExt = "vcf"; dlg.Filter = "VCF files (*.vcf)|*.vcf|All files (*.*)|*.*"; dlg.InitialDirectory = Path.GetFullPath(Path.Combine( Environment.CurrentDirectory, @"..\..\..\..\..\PDIFiles")); dlg.FileName = lblFilename.Text; if (dlg.ShowDialog() == DialogResult.OK) { try { this.Cursor = Cursors.WaitCursor; // Enforce UTF-8 encoding if using vCard 4.0 if ((!miFileUnicode.Checked || !miPropUnicode.Checked) && vCards.Any( c => c.Version == SpecificationVersions.vCard40)) { this.ChangeFileEncoding_Click(miFileUnicode, e); } // Open the file and write the vCards to it. We'll use the same encoding method used by // the parser. using (var sw = new StreamWriter(dlg.FileName, false, PDIParser.DefaultEncoding)) { vCards.WriteToStream(sw); } lblFilename.Text = dlg.FileName; wasModified = false; } catch (Exception ex) { string error = String.Format("Unable to save vCards:\n{0}", ex.Message); if (ex.InnerException != null) { error += ex.InnerException.Message + "\n"; if (ex.InnerException.InnerException != null) { error += ex.InnerException.InnerException.Message; } } System.Diagnostics.Debug.Write(ex); MessageBox.Show(error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { this.Cursor = Cursors.Default; } } } }
/// <summary> /// This handles various commands for the data grid /// </summary> /// <param name="source">The source of the event</param> /// <param name="e">The event arguments</param> protected void dgVCards_ItemCommand(object source, DataGridCommandEventArgs e) { VCardCollection vc = (VCardCollection)Session["VCards"]; switch (e.CommandName) { case "Add": vc.Add(new VCard()); Response.Redirect(String.Format("VCardDetails.aspx?Index={0}", vc.Count - 1)); break; case "Edit": if (e.Item.ItemIndex < vc.Count) { Response.Redirect(String.Format("VCardDetails.aspx?Index={0}", e.Item.ItemIndex)); } break; case "Download": if (vc.Count == 0) { lblMsg.Text = "No vCards to download"; } else { // This is only applicable for vCard 2.1 switch (cboPropEncoding.SelectedIndex) { case 0: BaseProperty.DefaultEncoding = new UTF8Encoding(false, false); break; case 1: BaseProperty.DefaultEncoding = Encoding.GetEncoding("iso-8859-1"); break; default: BaseProperty.DefaultEncoding = new ASCIIEncoding(); break; } // Send the file to the user this.Response.ClearContent(); this.Response.ContentType = "text/x-vcard"; this.Response.AppendHeader("Content-Disposition", "inline;filename=VCards.vcf"); // The collection can be written directly to the stream. Note that more likely than not, // it will be written as UTF-8 encoded data. vc.WriteToStream(Response.Output); Response.End(); } break; } }