WebClient request = new WebClient();
request.Credentials = new NetworkCredential("d","d");
request.DownloadFile("ftp://172.17.2.158/aa.xls",@"D:\abc.xls");FTP:用DownloadFile方法是否能下载?

解决方案 »

  1.   

    可以!
    request.DownloadFile("ftp://username:[email protected]/aa.xls",@"D:\abc.xls");
      

  2.   

    flyaqiao(孤风捉影) 谢谢,但是还是不好用
      

  3.   

    // 传一个uri对象进来就可以了
    public bool DownLoadFileFromServer(Uri serverUri)
            {
                string fileName = @"c:\aa.txt";       // Be saved local file            //*************************************************************
                // The serverUri parameter should start with the ftp:// scheme.
                //*************************************************************            
                if (serverUri.Scheme != Uri.UriSchemeFtp)
                {
                    return false;
                }
                //*************************************************************
                // Get the object used to communicate with the server.
                //************************************************************* 
                WebClient request = new WebClient();            //*************************************************************
                // This example assumes the FTP site uses anonymous logon.
                //************************************************************* 
                request.Credentials = new NetworkCredential("wangjianfei", "1");
                try
                {
                    request.DownloadFile(serverUri, fileName);        
                    byte[] newFileData = request.DownloadData(serverUri.ToString());
                    
                    //*************************************************************
                    // Display the downloaded data.
                    //*************************************************************
                    string download = Encoding.ASCII.GetString(newFileData);
                    Console.WriteLine(download);                string fileString = System.Text.Encoding.UTF8.GetString(newFileData);
                    Console.WriteLine(fileString);
                }
                catch (WebException e)
                {
                    Console.WriteLine(e.ToString());
                }
                return true;
            }