1

我的代码:

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=EC2003090503AA"。---> System.IO.DirectoryNotFoundException:找不到路径的一部分“/Users/valeria/Projects/screening/screening/bin/Debug/http:/www.broadinstitute.org/cmap/viewScan.jsp?type =CEL&scan=EC2003090503AA"

怎么了?

4

3 回答 3

2

转义的 URI 肯定没有帮助。URL 编码通常仅在您正在编码的项目被附加到 URL 时使用;对 URL 本身进行编码是不必要的,并且可能导致其他问题。

我强烈建议改变

  string remoteUri="http://www.broadinstitute.org%2Fcmap%2FviewScan.jsp%3Ftype%3DCEL%26scan%3D"+p;

  string remoteUri ="http://www.broadinstitute.org/cmap/viewScan.jsp?type=CEL&scan="+p;

并重试。

于 2013-07-25T22:02:22.413 回答
0

正如 Adrian Wragg 所指出的,变量单元格是错误的。

您的错误已经表明您的问题(粗体部分是您的单元格变量中的内容)“/Users/valeria/Projects/screening/screening/ bin/Debug/http:/www.broadinstitute.org/cmap/viewScan.jsp?type =CEL&scan=EC2003090503AA "

因此,请确保您提供有效的路径。

如果你不相信我,你可以像这样检查你的文件路径:

If (!System.IO.Directory.Exists(dir))
{
Stop; //<== if it hits here, we are right. ;-)
}
于 2013-07-25T21:59:16.793 回答
0

您的代码有两个问题:

1:您正在使用编码的 Uri,因此您必须使用 System.Web.HttpUtility 解码您的 Uri:

string decodedUri = HttpUtility.UrlDecode(remoteUri);

然后,您将获得正确的 Uri:

http://www.broadinstitute.org/cmap/viewScan.jsp?type=CEL&scan=EC2003090503AA

你应该传递给myWebClient

myWebClient.DownloadFile(decodedUri, pFileName);

2:你的cell变量指向url,所以你必须修复它。您可以将string.Empty其临时分配或删除,以查看该解决方案是否有效。

于 2013-07-25T22:12:41.953 回答