我正在将文件从ftp下载到我的本地文件夹。 在程序中间我经常出错:在我的本地文件夹中拒绝访问路径:c:\ users \ Project。
注意:我在管理员
下运行我几乎没有尝试在任何地方搜索,但我找不到解决方案。
Getting the Error at: FileStream localFileStream = new FileStream(localFile, FileMode.Create);
Here is the code:-
class Program
{
static void Main(string[] args)
{
/* Create Object Instance */
ftp ftpClient = new ftp(@"ftp://xxxxxxx/", "user123", "pass123");
/* Download a File */
ftpClient.download("werty.docx", @"C:\users\Project");
}
}
public ftp(string hostIP, string userName, string password)
{ host = hostIP; user = userName; pass = password; }
/* Download File */
public void download(string remoteFile, string localFile)
{
try
{
/* Create an FTP Request */
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
/* Log in to the FTP Server with the User Name and Password Provided */
ftpRequest.Credentials = new NetworkCredential(user, pass);
/* When in doubt, use these options */
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
/* Specify the Type of FTP Request */
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
/* Establish Return Communication with the FTP Server */
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
/* Get the FTP Server's Response Stream */
ftpStream = ftpResponse.GetResponseStream();
/* Open a File Stream to Write the Downloaded File */
FileStream localFileStream = new FileStream(localFile, FileMode.Create);
// FileSystemInfo
/* Buffer for the Downloaded Data */
byte[] byteBuffer = new byte[bufferSize];
int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
/* Download the File by Writing the Buffered Data Until the Transfer is Complete */
try
{
while (bytesRead > 0)
{
localFileStream.Write(byteBuffer, 0, bytesRead);
bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
}
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
/* Resource Cleanup */
localFileStream.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
return;
}