C# 下载文件C时出错#
我的代码:C# 下载文件C时出错#,c#,system.net,system.net.webexception,C#,System.net,System.net.webexception,我的代码: string dir = "/Users/valeria/Desktop/screening/"+cell; string remoteUri ="http://www.broadinstitute.org%2Fcmap%2FviewScan.jsp%3Ftype%3DCEL%26scan%3D"+p; string pFileName = dir + "/p"; using (WebClient myWebClient = new
string dir = "/Users/valeria/Desktop/screening/"+cell;
string remoteUri ="http://www.broadinstitute.org%2Fcmap%2FviewScan.jsp%3Ftype%3DCEL%26scan%3D"+p;
string pFileName = dir + "/p";
using (WebClient myWebClient = new WebClient())
{
myWebClient.DownloadFile(remoteUri, pFileName);
}
我的程序创建了一个文件pFileName
,但没有下载任何内容,因为我遇到以下异常:
未处理的异常:System.Net.WebException:找不到的一部分
路径
“/Users/valeria/Projects/screening/screening/bin/Debug/http:/www.broadinstitute.org/cmap/viewScan.jsp?type=CEL&scan=ec200309053aa”。
--->System.IO.DirectoryNotFoundException:找不到路径的一部分
“/Users/valeria/Projects/screening/screening/bin/Debug/http:/www.broadinstitute.org/cmap/viewScan.jsp?type=CEL&scan=ec200309053aa”
怎么了?正如阿德里安·拉格所指出的,变量单元格是错误的 您的错误已经表明您的问题(粗体部分是单元格变量中的内容) “/Users/valeria/Projects/screening/screening/bin/Debug/http:/www.broadinstitute.org/cmap/viewScan.jsp?type=CEL&scan=ec200309053aa” 因此,请确保提供有效的路径 如果你不相信我,你可以像这样检查你的文件路径:
If (!System.IO.Directory.Exists(dir))
{
Stop; //<== if it hits here, we are right. ;-)
}
如果(!System.IO.Directory.Exists(dir))
{
Stop;//转义的URI肯定没有帮助。URL编码通常仅在您正在编码的项被附加到URL时使用;对URL本身进行编码是不必要的,并且可能导致其他问题
我强烈建议改变
string remoteUri="http://www.broadinstitute.org%2Fcmap%2FviewScan.jsp%3Ftype%3DCEL%26scan%3D"+p;
到
然后重试。您的代码有两个问题:
1:您使用的是编码的Uri,因此必须使用System.Web.HttpUtility对Uri进行解码:
string decodedUri = HttpUtility.UrlDecode(remoteUri);
然后,您将获得适当的Uri:
您应该将其传递到myWebClient
:
myWebClient.DownloadFile(decodedUri, pFileName);
2:您的
单元格
变量指向url,因此您必须修复它。您可以将其指定为字符串。清空
或临时删除它,以查看该解决方案是否有效。您可能希望查看变量“cell”的值。@AdrianWragg一切正常。程序创建文件。当我不使用di时,单元格只是字符串r和jus编写myWebClient.DownloadFile(remoteUri,“file.txt”);我收到了同样的错误太棒了!非常感谢!@FabianBigler-我第一次直接读过了,别担心-然后,我尝试在控制台应用程序中运行它!@AdrianWragg,这很公平。我认为它与文件名有关,因为错误表明了这一点。;)
myWebClient.DownloadFile(decodedUri, pFileName);