WebClient DownloadFile Invalid characters in the path

I am a beginner, so I am sure that this is something really basic that I am missing.

I have a simple program to run through a csv file that contains links to images for saving these images to the specified location of the save file.

I am parsing a cell containing url into List<string[]> .

If I put GetImage(@"http://www.example.com/picture.jpg", 1) , then my GetImage function will work as it should. When I try to use a loop and pass str[0] in the variable, I get an error message regarding invalid characters in the path.

I used the MessageBox to tell me what the difference is, and as far as I can tell, when I pass str[0] to the function, it adds double quotes (ie "http: // www. Example.com" is displayed instead http://www.example.com as this is when I just send one line.

What am I doing wrong?

 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
source share
1 answer

No matter which function or method creates these quotes, you can replace them all.

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

I think your files contain quotation marks or their parseCSV method.

Update:

I used this code and it works without any problems and without quotes:

 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); } 
+2
source

Source: https://habr.com/ru/post/1439647/


All Articles