2011-12-11 6 views
0

私はC#で新しいです。ローカルマシン上のドキュメントからCSVファイルを開くためのコードを記述しました。うまく動作し、データ解析が機能します。問題は、インターネットサイトからファイルを開くコードを変更したときに、動作させることができないことです。私はVBAを使ってこのファイルを開くことができますが、今はC#ADO.NETを使いたいと思います。私はGoogleで検索して答えを見つけることができません。誰でもコードを助けたり、良いチュートリアルでウェブサイトを紹介したりすることができます。すべての助けを非常に感謝します。コードが添付されている、私は問題が24行目から26行目までであると確信しています。C#を使用してインターネットからCSVファイルを開くことができません

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
using System.Net; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // 
      // Read in a file line-by-line, and store it all in a List. 
      // 
      int i = 0; 
      DateTime dte; 
      List<string> list = new List<string>(); 
      float[] Prices = new float[4]; 

      WebClient wc = new WebClient(); 

      byte[] data = wc.DownloadData("http://www.datasource.com/apps/qt/csv/pricehistory.ac?section=yearly_price_download&code=XXX"); 

      using (StreamReader reader = new StreamReader(wc)) 
      { 
       string line; 
       while ((line = reader.ReadLine()) != null) 
       { 
        //list.Add(line); // Add to list. 
        Console.WriteLine(line); // Write to console. 

        string[] parts = line.Split(','); 
        int DateSetter = 1; 
        int DateDone = 0; 
        int CountFloat = 0; 
        int PricesDone = 0; 
        Double Volume = 0; 

        foreach (string part in parts) 
        { 
         Console.WriteLine("{0} : {1}", i, part); 

         if (DateSetter == 1) 
         { 
          dte = DateTime.Parse(part); 
          DateSetter = 2; 
          Console.WriteLine(dte); 
         } 
         if (DateDone == 1) 
         { 
          if (DateSetter < 6) 
          { 
           Prices[CountFloat] = float.Parse(part); 
           CountFloat++; 
           DateSetter++; 
           Console.WriteLine(Prices[3]); 
          } 
         } 
         DateDone = 1; 
         if (PricesDone == 1) 
         { 
          Volume = double.Parse(part); 
          Console.WriteLine(Volume); 
         } 
         if (DateSetter == 6) 
         { 
          PricesDone = 1; 
         } 
        } 

       } 
      } 
      Console.ReadLine(); 
     } 
    } 
} 
+0

例外の詳細やエラーメッセージは一切含めませんでした。プロキシの背後にいる場合は、その理由が考えられます。プログラムが外部ファイルにアクセスするためには、資格情報を使用する必要があります。 – GR7

+0

ちょうど "それを働かせることができません"は、問題の説明としては役に立ちません。コードを実行するとどうなりますか、それはあなたが期待しているものとどう違うのですか?あなたは腹が立っていますか?その場合はどちらですか? – Guffa

+1

正確なソースコードは確実でしょうか? 'StreamReader'の' ctor'は 'WebClient'のためのオーバーロードを持っていません。 – ordag

答えて

0

貼り付けたコードはコンパイルされません。ただし、その後の行に文字列を分割し、文字列にダウンロードするWebClientを使用することができます。

string content; 
using(WebClient wc = new WebClient()) 
content = wc.DownloadString("http://www.datasource.com/apps/qt/csv/pricehistory.ac?section=yearly_price_download&code=XXX"); 

foreach(var line in content.Split(new string [] {Environment.NewLine}, StringSplitOptions.None)) 
{ 
    //... 
} 
+0

ありがとうございました – Tim

+0

もしティムの答えが助けられたら、答えとしてマークしてください。 – GR7

0

別のオプションは、あなたがやっているように、データをダウンロードし、MemoryStreamでそれをラップすることです:

WebClient wc = new WebClient(); 
byte[] data = wc.DownloadData(
    "http://www.datasource.com/apps/qt/csv/pricehistory.ac?section=yearly_price_download&code=XXX"); 

using (var ms = new MemoryStream(data)) 
{ 
    using (var reader = new StreamReader(ms)) 
    { 
     string line; 
     while ((line = reader.ReadLine()) != null) 
     { 
      // do whatever 
     } 
    } 
} 

この文字列を分割する利点は、かなり少ないメモリを使用することです。

関連する問題