我正在尝试用ftp协议从树莓派下载文件。我必须按下一个按钮来下载文件,按下按钮,访问"C:UsersMonsterDesktopftp"被拒绝。我得到一个错误。我安装了应用程序清单,为admin添加了必要的权限行,右键单击我要下载的文件夹并检查安全部分的权限,所有权限都是打开的。我找不到错误的原因,你能帮助我吗,祝你有一个美好的一天。
int bufferSize = 2048;
FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create("ftp://10.252.26.72//media/pi/Video1/video.mp4");
ftpRequest.Credentials = new NetworkCredential("pi", "1");
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
Stream ftpStream = ftpResponse.GetResponseStream();
FileStream localFileStream = new FileStream(@"C:UsersMonsterDesktopftp", FileMode.Create);
byte[] byteBuffer = new byte[bufferSize];
int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
try
{
while (bytesRead > 0)
{
localFileStream.Write(byteBuffer, 0, bytesRead);
bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
}
}
catch (Exception ex)
{
}
这很可能是因为您没有使用using
处理文件流。您还应该设置FileAccess
模式。
循环也是不必要的,你可以直接使用CopyTo
。
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create("ftp://10.252.26.72//media/pi/Video1/video.mp4")
ftpRequest.Credentials = new NetworkCredential("pi", "1");
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
using (FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse())
using (Stream ftpStream = ftpResponse.GetResponseStream())
using (FileStream localFileStream = new FileStream(@"C:UsersMonsterDesktopftp", FileMode.Create, FileAccess.Write, FileShare.None))
{
ftpStream.CopyTo(localFileStream);
}