Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 路径错误c中的非法字符#_C# - Fatal编程技术网

C# 路径错误c中的非法字符#

C# 路径错误c中的非法字符#,c#,C#,我试图从下面的链接下载csv文件,但在最后一行抛出了一个异常“路径错误中的非法字符”。我相信是链接中的问号把一切都搞砸了,但我必须让它工作起来。。有什么建议吗 string remoteUri = "download.finance.yahoo.com/d/quotes.csv?s=%40%5EDJI,aapl&f=o&e=.csv"; string fileName = "aapl.csv", myStringWebResource = null; // Create a ne

我试图从下面的链接下载csv文件,但在最后一行抛出了一个异常“路径错误中的非法字符”。我相信是链接中的问号把一切都搞砸了,但我必须让它工作起来。。有什么建议吗

string remoteUri = "download.finance.yahoo.com/d/quotes.csv?s=%40%5EDJI,aapl&f=o&e=.csv";
string fileName = "aapl.csv", myStringWebResource = null;
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename.
myStringWebResource = remoteUri + fileName;
// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(myStringWebResource, fileName);
这项工作:

string remoteUri = @"http://download.finance.yahoo.com/d/quotes.csv?s=%40%5EDJI,aapl&f=o&e=.csv";
string fileName = @"c:\aapl.csv";

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();       

// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(remoteUri, fileName);

嗯。让我们看看你的代码

// Dont forget the "http://". A lot of browser add it themselves but the WebClient doesnt.
string remoteUri = "download.finance.yahoo.com/d/quotes.csv?s=%40%5EDJI,aapl&f=o&e=.csv";

// I recommend to take the habitude to write each one in one line.
string fileName = "aapl.csv", myStringWebResource = null;

// Use the "using" keyword to dispose WebClient
WebClient myWebClient = new WebClient();

// Why are you doing this? Your url is working without. No need to concat here.
myStringWebResource = remoteUri + fileName;

// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(myStringWebResource, fileName);
测试解决方案:(在上演示)

您问题的解决方案:

using System;
using System.Net;

public class Program
{
    public void Main()
    {
        string remoteUri = "http://download.finance.yahoo.com/d/quotes.csv?s=%40%5EDJI,aapl&f=o&e=.csv";
        string fileName = "aapl.csv";

        using (var myWebClient = new WebClient())
        {
            myWebClient.DownloadFile(remoteUri, fileName);
        }   
    }
}

您可以用另一个字符替换问号…问号是有效的url字符。为什么您的uri不以协议开头?myStringWebResource是“download.finance.yahoo.com/d/quotes.csv?s=%40%5EDJI,aapl&f=o&e=.csvaapl.csv”这似乎不正确。我认为您的问题是
mystringwebsource=remoteUri+fileName,只是不要这样做。然后将最后一行替换为
myWebClient.DownloadFile(remoteUri,fileName)不要忘记处理WebClient。看看我的答案。
using System;
using System.Net;

public class Program
{
    public void Main()
    {
        string remoteUri = "http://download.finance.yahoo.com/d/quotes.csv?s=%40%5EDJI,aapl&f=o&e=.csv";
        string fileName = "aapl.csv";

        using (var myWebClient = new WebClient())
        {
            myWebClient.DownloadFile(remoteUri, fileName);
        }   
    }
}