如何从FTP服务器下载文件

时间:2014-06-01 14:03:27

标签: c# ftp

我正在尝试下载文件(各种文件,exe dll txt等)。当我尝试运行它时会出现错误:

using (FileStream ws = new FileStream(destination, FileMode.Create))

这是错误消息:

Access to the path 'C:\Riot Games\League of Legends\RADS\solutions  
\lol_game_client_sln\releases\0.0.1.41\deploy'(which is my destination, where I want 
to save it) is denied.

这是我的代码

void download(string url, string destination)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
        request.Method = WebRequestMethods.Ftp.DownloadFile;
        request.Credentials = new NetworkCredential("user", "password");
        request.UseBinary = true;

        using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
        {
            using (Stream rs = response.GetResponseStream())
            {
                using (FileStream ws = new FileStream(destination, FileMode.Create))
                {
                    byte[] buffer = new byte[2048];
                    int bytesRead = rs.Read(buffer, 0, buffer.Length);

                    while (bytesRead > 0)
                    {
                        ws.Write(buffer, 0, bytesRead);
                        bytesRead = rs.Read(buffer, 0, buffer.Length);
                    }
                }
            }
        }

2 个答案:

答案 0 :(得分:2)

错误明确且清晰:您对C:\Riot Games\League of Legends\RADS\solutions\lol_game_client_sln\releases\0.0.1.41\deploy

没有写入权限

您的程序需要此权限才能将文件写入指定的位置。

答案 1 :(得分:0)

当他说手动创建文件时,右键单击文件夹 - >新建>文本文件或任何您想要创建的文件,只是为了检查是否可以。如果没有,请尝试检查父目录的权限。