fun*_*err 0 c# file-permissions webclient filestream
我写了这段代码从FTP服务器下载文件.当程序尝试下载并保存文件时,我收到拒绝访问错误.我尝试使用管理员权限打开程序,但它给出了同样的错误.
WebClient request = new WebClient();
request.Credentials = new NetworkCredential(txtFTPuser.Text,
txtFTPpassword.Text);
/*List all directory files*/
byte[] fileData = request.DownloadData(fullDownloaPath);//dnoces.dreamhost.com
FileStream fi = File.Create(downloadTo);
fi.Write(fileData, 0, fileData.Length);
fi.Close();
MessageBox.Show("Completed!");
Run Code Online (Sandbox Code Playgroud)

您需要在通话中提供完整的文件路径File.Create.现在,你正试图用你正在下载的文件覆盖你的"游戏"目录,这并不好.
尝试设置downloadTo为类似的东西C:\Users\agam\Desktop\Games\myfile.ext而不是,因为它可能设置为现在,C:\Users\agam\Desktop\Games\.
顺便说一句,我鼓励您查看代码有两个明显的改进:
DownloadFile方法WebClient,而不是DownloadData为您节省一些精力.using调用中包装WebClient的创建,以确保即使您的方法遇到异常也将其关闭.否则,您可能会泄漏客户持有的资源.例如:
using (WebClient request = new WebClient())
{
request.Credentials = new NetworkCredential(txtFTPuser.Text,
txtFTPpassword.Text);
request.DownloadFile(fullDownloadPath, downloadTo);
MessageBox.Show("Completed!");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3073 次 |
| 最近记录: |