void DownloadNext() { PostInfo pi = this.projects[0].PostList[0]; //存数据库 this.SaveTitle2DB(pi); while (this.projects[0].ExitsFileUrl.Contains(pi.FileList[0]) && pi.FileList.Count > 0) { pi.FileList.Remove(pi.FileList[0]); } KFileInfo kfi = new KFileInfo(); kfi.PostId = pi.PostId; kfi.Url = pi.FileList[0]; Uri fileUri = new Uri(kfi.Url); string savePath = Path.Combine( string.Format("{0}_{1}", pi.PostId, pi.Title), string.Format("{0}_{1}.{2}", fileUri.LocalPath, Guid.NewGuid()) ); string saveName = Path.Combine( this.saveFolder, savePath ); this.wc.DownloadFileAsync(fileUri, saveName, kfi); }
/// <summary> /// 方案1:直接保存数据库,以后下载 /// </summary> internal void SaveData() { this.PostFetchInfoChanged("开始整理", "开始整理", "", null); int postId = -1; this.fileList = new Dictionary <string, KFileInfo>(); KFileInfo fi; SQLiteConnection conn = new SQLiteConnection(DbHelper.ConnectionString); //conn.ConnectionString = Program.configPath; conn.Open(); using (SQLiteTransaction trans = conn.BeginTransaction()) { #region 循环所有post及其下面的file,写入数据库 foreach (PostInfo pi in this.projects[0].PostList) { //存入当前Post到数据库 //插入Posts表, 返回postId SaveTitle2DB(pi); if (pi.PostId < 1) { string message = string.Format("插入Post失败:{0},跳过当前Post文件列表!!!" + pi.Url); this.PostFetchInfoChanged("插入Post失败", message, "", null); continue; } //循环当前Post下的所有文件 foreach (string url in pi.FileList) { fi = new KFileInfo(); #region 插入Files表,返回fileId if (this.fileList.ContainsKey(url)) { string message = string.Format("存在的Url :{0}", url); this.PostFetchInfoChanged("已存在的文件", message, "", null); fi.Id = this.fileList[url].Id; } else { string sqlFile = "INSERT INTO files(`url`) VALUES(@url);select last_insert_rowid()"; DbParameter[] pramsFile = { DbHelper.MakeInParam("@url", DbType.String, 500, url) }; try { fi.Id = Convert.ToInt32( DbHelper.ExecuteScalar( trans, CommandType.Text, sqlFile, pramsFile ) ); this.fileList.Add(url, fi); } catch (Exception ex) { fi.Id = -1; } } #endregion #region 插入File2Post表 try { if (fi.Id > 0) { string sqlFile2Post = string.Format( "INSERT INTO file2post(`postid`,`fileid`) VALUES({0},{1})", postId, fi.Id ); DbHelper.ExecuteNonQuery( trans, CommandType.Text, sqlFile2Post ); } else { this.PostFetchInfoChanged("插入文件失败", "插入文件失败:" + url, "", null); } } catch (Exception ex) { if (this.SaveDataComplted != null) { this.SaveDataComplted( this, new FetchPostsAndFilesCompletedEventArgs(this.projects, ex, false, null) ); } } #endregion } } #endregion trans.Commit(); } conn.Close(); if (this.SaveDataComplted != null) { this.SaveDataComplted( this, new FetchPostsAndFilesCompletedEventArgs(this.projects, null, false, null) ); } }