Frank La Vigne

Fear and Loathing in .NET

MVP Logo
Tablet PC MVP

Social Networks

Subscription Options

Add to Google

Subscribe in Bloglines

My Links


Post Categories

Archives

Image Galleries

Cached @ 9/3/2010 2:44:06 AMControl ASP.skins_marvin3_controls_archivelinks_ascx

GamerTag

Dev Community Events

Blog Stats

Cached @ 9/3/2010 2:44:06 AMControl ASP.skins_marvin3_controls_blogstats_ascx  

News


Blog Roll

Favorite Sites

Gadget Blogs

Tablet PC Links

Cached @ 9/3/2010 2:44:06 AMControl ASP.skins_marvin3_controls_categorydisplay_ascx

PDC09 Downloader Source Code

It may not be pretty source code, but I thought I’d post the source code here in case someone wanted to see how I did it.

As noted in the source, the LINQ to XML code can use some tidying up, but I wrote this late at night while building baby furniture.

There are two files of interest here: a Session object to store PDC session information and the code behind for my WPF window.

Session.cs file

   1: public string ID { get; set; }
   2: public string Title { get; set; }
   3: public string Speaker { get; set; }
   4: public Uri WMVHighUri { get; set; }

 

Window1.xaml.cs

   1: StringBuilder _errors = new StringBuilder();
   2: private string _destinationPath = string.Empty;
   3:  
   4: private const string FILENAME_FORMATSTRING = "{0}\\{1}_{2}.wmv";
   5: private const string DATA_FILE_URI = "http://www.franksworld.com/Downloads/PDC09Sessions.xml";
   6:  
   7:  
   8: public Window1()
   9: {
  10:     InitializeComponent();
  11: }
  12:  
  13: private void Window_Loaded(object sender, RoutedEventArgs e)
  14: {
  15:     this._destinationPath = PickDownloadDirectory();
  16:  
  17:     if (this._destinationPath == null)
  18:     {
  19:         Application.Current.Shutdown();
  20:     }
  21:  
  22:  
  23: }
  24:  
  25:  
  26: private void btnStart_Click(object sender, RoutedEventArgs e)
  27: {
  28:     this.lblStatus.Content = "Downloading Data File";
  29:  
  30:     XDocument dataFile = XDocument.Load(DATA_FILE_URI);
  31:  
  32:     var sessionNodes =  dataFile.Descendants("session");
  33:  
  34:  
  35:     DownLoadFiles(sessionNodes);
  36:  
  37:  
  38:  
  39: }
  40:  
  41:  
  42: private string PickDownloadDirectory()
  43: {
  44:  
  45:     string returnDirectory = null;
  46:  
  47:  
  48:     Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
  49:     dlg.FileName = "";
  50:     dlg.DefaultExt = ".*";
  51:     dlg.Filter = "All documents (*.*)|*.*";
  52:  
  53:     // Show open file dialog box
  54:     Nullable<bool> result = dlg.ShowDialog();
  55:  
  56:     // Process open file dialog box results
  57:     if (result == true)
  58:     {
  59:         // Open document
  60:         string filename = dlg.FileName;
  61:  
  62:  
  63:         returnDirectory = System.IO.Path.GetDirectoryName(filename);
  64:  
  65:  
  66:     }
  67:  
  68:     return returnDirectory;
  69:  
  70: }
  71:  
  72:  
  73: private void DownLoadFiles(IEnumerable<XElement> sessionNodes)
  74: {
  75:     var sessionList = from session in sessionNodes
  76:             select new Session
  77:             {
  78:                 SessionId = (string)session.Element("code"),
  79:                 WMVHigh = GetUri( session.Descendants("resources") , "WMVHigh" ),
  80:                 Name = GetSessionName(session)
  81:             };
  82:  
  83:     sessionList.ToList().ForEach(x => DownLoadFile(x));
  84:  
  85:  
  86:  
  87:     MessageBox.Show(_errors.ToString());
  88:  
  89:  
  90:  
  91:  
  92:  
  93:  
  94: }
  95:  
  96: private string GetSessionName(XElement session)
  97: {
  98:  
  99:     // THIS is *UGLY* LINQ code. I need to fix
 100:     var resources = session.Descendants("resources");
 101:  
 102:     var webResource = resources.Where(x => x.Attribute("ContentType").Value == "Web");
 103:  
 104:     string name = string.Empty;
 105:  
 106:     webResource.ToList().ForEach(x => name = x.Value);
 107:  
 108:     return name;
 109:  
 110: }
 111:  
 112: private void DownLoadFile(Session session)
 113: {
 114:  
 115:     try
 116:     {
 117:         WebClient wc = new WebClient();
 118:  
 119:         string destinationFileName = string.Format(FILENAME_FORMATSTRING, _destinationPath, session.SessionId, session.Name);
 120:  
 121:         if (!File.Exists(destinationFileName))
 122:         {
 123:             this.lblStatus.Content = "Copying " + destinationFileName;
 124:             wc.DownloadFile(session.WMVHigh, destinationFileName);
 125:         }
 126:     }
 127:     catch (Exception ex)
 128:     {
 129:  
 130:         _errors.AppendLine("session: " + session.SessionId + ": error: " +  ex.Message);
 131:     }
 132: }
 133:  
 134: private string GetUri(IEnumerable<XElement> iEnumerable, string p)
 135: {
 136:  
 137:     var wmvnode = iEnumerable.Where(node => node.Attribute("ContentType").Value == p);
 138:  
 139:     return wmvnode.Attributes("uri").First().Value;
 140:  
 141: }

 

 

Technorati Tags: ,,,

posted on Friday, November 20, 2009 6:34 PM

============ Debug Build ============
Dottext Version: 0.95.2004.102
Machine Name: IIS07902
.NET Version: 2.0.50727.3053
No User
============ Debug Build ============