Pergunta

My code:

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);

            }

My program creates a file pFileName, but doesn't download anything because I get the following exception:

Unhandled Exception: System.Net.WebException: Could not find a part of the path "/Users/valeria/Projects/screening/screening/bin/Debug/http:/www.broadinstitute.org/cmap/viewScan.jsp?type=CEL&scan=EC2003090503AA". ---> System.IO.DirectoryNotFoundException: Could not find a part of the path "/Users/valeria/Projects/screening/screening/bin/Debug/http:/www.broadinstitute.org/cmap/viewScan.jsp?type=CEL&scan=EC2003090503AA"

What's wrong?

Foi útil?

Solução

The escaped URI certainly isn't helping. URL-encoding is generally only used when the item you are encoding is being appended to a URL; encoding the URL itself is unnecessary and can lead to other problems.

I would strongly suggest changing

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

to

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

and re-trying.

Outras dicas

The variable cell is - as pointed out by Adrian Wragg - wrong.

Your error already indicates your problem (the bold part is what's in your cell-variable) "/Users/valeria/Projects/screening/screening/bin/Debug/http:/www.broadinstitute.org/cmap/viewScan.jsp?type=CEL&scan=EC2003090503AA"

So make sure you provide a valid path.

If you don't believe me, you can check your filepath like this:

If (!System.IO.Directory.Exists(dir))
{
Stop; //<== if it hits here, we are right. ;-)
}

There are two problems with your code:

1: You are using encoded Uri, so you have to decode your Uri using System.Web.HttpUtility:

string decodedUri = HttpUtility.UrlDecode(remoteUri);

Then, you will get proper Uri:

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

Which you should pass to myWebClient:

myWebClient.DownloadFile(decodedUri, pFileName);

2: Your cell variable points to url, so you have to fix it. You can assign it as string.Empty or remove it temporary to see if that solution works.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top