private async Task LoadSageAsync(string location) { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); Encoding encoding = Encoding.GetEncoding(1252); StreamReader reader = new StreamReader(location, encoding, true); string line; //Index Colluns int index = 0; int RefIndex = 0; int NomeIndex = 1; int PvpIndex = 2; int IvaIndex = 5; int StockIndex = 3; try { while ((line = await reader.ReadLineAsync()) != null) { string[] cols = line.Split(','); if (cols.Length > 5) { //if (index == 0) //{ //RefIndex = cols.ToList().FindIndex(0, cols.Length, x => x == "REF"); //NomeIndex = cols.ToList().FindIndex(0, cols.Length, x => x == "PRODUTO"); //PvpIndex = cols.ToList().FindIndex(0, cols.Length, x => x == "PVP"); //IvaIndex = cols.ToList().FindIndex(0, cols.Length, x => x == "IMPOSTO"); //StockIndex = cols.ToList().FindIndex(0, cols.Length, x => x == "DISPONÍVEL"); //} //else //{ Produto p = new Produto(); p.REF = cols[RefIndex].Replace("\"", ""); p.Nome = cols[NomeIndex].Replace("\"", ""); float preco = float.NaN; if (float.TryParse(cols[PvpIndex].Replace('.', ','), out preco)) { p.Preco_cIVA = preco; } //p.IVA = cols[IvaIndex]; int stockSage = 0; if (int.TryParse(cols[StockIndex], out stockSage)) { p.StockSage = stockSage; } SageProdutos.Add(p); //} } index++; Dispatcher.Invoke(() => UC_StatusBar_ListObjects.Text = string.Format("Produtos: {0}", Produtos.Count)); } } catch (Exception ex) { } }
private async Task LoadDataBase(string webLocation) { Produtos.Clear(); SageProdutos.Clear(); string webExtension = System.IO.Path.GetExtension(webLocation); string sageLocation = @"\\srvmm\USR\MeioMundo_Local\Listagem de Artigos _EUROS_.TXT"; string serverPath = @"srvmm"; string sageExtension = System.IO.Path.GetExtension(sageLocation); if (webExtension.ToUpper() == ".CSV" && sageExtension.ToUpper() == ".TXT") { await LoadWebSiteAsync(webLocation); try { using (Network.NetworkShareAccesser.Access(serverPath, "meiomundo", "meiomundo")) { await Task.Run(() => LoadSageAsync(sageLocation)); } } catch (Exception ex) { OpenFileDialog WebDialog = new OpenFileDialog(); WebDialog.Filter = "Website Files|*.csv;*.txt|" + "All Files |*.*"; WebDialog.Title = "Carregar ficheiro com os dados do Site"; if (WebDialog.ShowDialog() == true) { webLocation = WebDialog.FileName; await LoadSageAsync(webLocation); } } Task task = new Task(() => UpdateStockAsync()); task.Start(); } }