2011-06-29 74 views
0

我编写了一个程序,使用WebClient.DownloadFile()从网站下载文件。下载文件错误

public static void downWeb() 
{ 
    WebClient myWebClient = new WebClient(); 
    path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); 
    if (!Directory.Exists(path)) 
    { 
     Directory.CreateDirectory(path); 
    }    
    if (add() == 1) 
    { 
     Console.WriteLine("Response is " + add());   
     Console.WriteLine("Downloading File = " + dynFileName + "...."); 
     myWebClient.DownloadFile(fullAddress, (path + dynFileName)); 
    } 
} 

public static int add() 
{ 
    string url = fullAddress; 

    WebRequest webRequest = WebRequest.Create(url); 
    WebResponse webResponse; 
    try 
    { 
     webResponse = webRequest.GetResponse(); 
    } 
    catch 
    { 
     return 0; 
    } 
    return 1; 
} 

downWeb()是在Main()函数被调用的函数。

add()是测试服务器上文件可用性的函数。如果响应是肯定的,则返回值“1”。

fullAddress =文件必须下载的地址。每次在Main()中存在的循环中调用此函数之前都会发生变化。

当我开始我的申请,我要求用户:网页

1)输入网址进行下载即www.1234.com \ samplefiles \ PG-次数1.pdf

2)数(通过更改上面的文件名编号在一个循环中,因为其余的url在服务器上是相同的)

现在我的问题是当我下载文件,第一次文件下载完成,但第二次下载永远不会完成。它说“请求已计划”,并且我的申请关闭。

我不知道这里发生了什么。

这怎么解决?

+0

在重要事件上设置断点时,您会看到什么行为? –

回答