public static void ImportAllCSV() { var db = new StockSupportSystemEntities(); var stocks = db.Database.SqlQuery <stock>("select * from stock where id<700000 and id >=600000").ToList(); foreach (stock stk in stocks) { string code = stk.type + stk.id; string now_day = DateTime.Now.ToString("yyyy-MM-dd"); string now_datetime = now_day.Split('-')[0] + now_day.Split('-')[1] + now_day.Split('-')[2]; string url = string.Format("http://quotes.money.163.com/service/chddata.html?code={0}&start=19900101&end={1}&fields=TCLOSE;LOW;HIGH;TOPEN;LCLOSE;CHG;PCHG;VOTURNOVER", code.Trim(), now_datetime); HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest; request.Method = "GET"; request.ProtocolVersion = new Version(1, 1); HttpWebResponse response = request.GetResponse() as HttpWebResponse; if (response.StatusCode == HttpStatusCode.NotFound) { return; } // 转换为byte类型 System.IO.Stream stream = response.GetResponseStream(); if (Directory.Exists(StockInfoRootPath)) { if (!File.Exists(StockInfoRootPath + "\\" + code.Trim() + ".CSV")) { MyFileHelper.CreateFileContent(StockInfoRootPath + "\\" + code.Trim() + ".CSV", ""); } //创建本地文件写入流 Stream fs = new FileStream(StockInfoRootPath + "\\" + code.Trim() + ".CSV", FileMode.Create); byte[] bArr = new byte[2048]; int size = stream.Read(bArr, 0, (int)bArr.Length); while (size > 0) { fs.Write(bArr, 0, size); size = stream.Read(bArr, 0, (int)bArr.Length); } fs.Close(); stream.Close(); } } }
protected override void OnStartup(StartupEventArgs e) { // 定义应用程序启动时要处理的内容 if (MyFileHelper.IsExistFile(XmlHelper.PersonXmlPath)) { MyFileHelper.ClearFile(XmlHelper.PersonXmlPath); } else { MyFileHelper.CreateFileContent(XmlHelper.PersonXmlPath, ""); } if (!MyFileHelper.IsExistFile(FileHelper.dbpath)) { if (!MyFileHelper.IsExistDirectory(FileHelper.dbfilepath)) { MyFileHelper.CreateDirectory(FileHelper.dbfilepath); } bool result = MyFileHelper.CopyFile(FileHelper.sourcedbpath, FileHelper.dbpath); } }