4

私は初心者なので、これは私が見逃している本当に基本的なものだと確信しています。

csv画像へのリンクを含むファイルを実行して、指定された保存ファイルの場所に画像を保存する簡単なプログラムがあります。

を含むセルを に解析していurlますList<string[]>

私がGetImage(@"http://www.example.com/picture.jpg", 1)自分の機能を配置すると、本来のGetImage機能が実行されます。ループを使用して変数を渡そうとすると、パスにstr[0]不正な文字があるというエラーが表示されます。

私は a を使用しMessageBoxて違いが何であるかを教えてくれました。私が知る限り、関数に を渡すと、二重引用符が追加されます (つまり、 httpstr[0]の代わりに "http://www.example.com" が表示されます)。1 つの文字列を送信するだけの場合は://www.example.comです。

私は何を間違っていますか?

private void button2_Click(object sender, EventArgs e)
    {
        string fileName = textBox1.Text;            
        folderBrowserDialog1.ShowDialog();
        string saveLocation = folderBrowserDialog1.SelectedPath;
        textBox2.Text = saveLocation;            
        List<string[]> file = parseCSV(fileName);
        int count = 0;
        foreach (string[] str in file)
        {
            if (count != 0)
            {                                                          
                GetImage(str[0], str[4]);                    
            }
            count++;
        }
        //GetImage(@"http://www.example.com/picture.jpg", "1");
    }


    private void GetImage(string url, string prodID)
    {   
        string saveLocation = textBox2.Text + @"\";;
        saveLocation += prodID + ".jpg";                        
        WebClient webClt = new WebClient();
        webClt.DownloadFile(url, saveLocation);                         
    }
4

1 に答える 1

2

これらの引用符を作成する関数またはメソッドに関係なく、それらすべてを置き換えることができます。

String myUrl = str[0];
myUrl = myUrl.Replace("\"", "");
GetImage(myUrl, str[4]);

ファイルに引用符が含まれているか、parseCSV メソッドがそれらを作成していると思います。

アップデート:

私はこのコードを使用しましたが、引用符なしでまったく問題なく動作します:

static void Main(string[] args)
{
  string fileName = "Test";
  //folderBrowserDialog1.ShowDialog();
  string saveLocation = ".\\";
  //textBox2.Text = saveLocation;
  List<string[]> file = new List<string[]>
  {
    new string[] { "http://www.example.com", "1", "1", "1", "1"},
    new string[] { "http://www.example.com", "2", "2", "2", "2"},
  };
  int count = 0;
  foreach (string[] str in file)
  {
    if (count != 0)
    {
        GetImage(str[0], str[4]);
    }
    count++;
  }
//GetImage(@"http://www.example.com/picture.jpg", "1");
}


private static void GetImage(string url, string prodID)
{
  string saveLocation = ".\\"; // textBox2.Text + @"\"; ;
  saveLocation += prodID + ".jpg";
  WebClient webClt = new WebClient();
  Console.WriteLine(url);
  webClt.DownloadFile(url, saveLocation);
}
于 2012-10-14T14:11:59.447 に答える