-1

I am trying to download files from Raspberry Pi with ftp protocol. I have to press a button to download a file Press the button and access to 'C:\Users\Monster\Desktop\ftp' is denied.' I'm getting an error. I installed the app manifest, added the necessary permission line for admin, right clicked on the folder I want to download and checked the permissions in the security section, all permissions are open. I couldn't find the reason for the error I got, can you help me, I wish you a good day.

 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:\Users\Monster\Desktop\ftp", 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)
        {
        }
4
  • Probably because you didn't close the file when you last tried it. Your localFileStream needs a using. Likewise ftpResponse and ftpStream also need using. Also: don't bother with the while, just use ftpStream.CopyTo(localFileStream); And don't swallow exceptions. Commented Jul 19, 2022 at 14:26
  • is ftp a directory? Commented Jul 19, 2022 at 14:29
  • FTP is the name of the internet protocol I use.
    – furkank00
    Commented Jul 19, 2022 at 15:48
  • I am not sure. But I don't get an error about the connection in the program, I just get an error about the directory where the file should be printed.
    – furkank00
    Commented Jul 20, 2022 at 16:17

1 Answer 1

1

Most likely this is because you are not disposing the file stream with using. You should also set the FileAccess mode.

Also the loop is unnecessary, you can just use 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:\Users\Monster\Desktop\ftp", FileMode.Create, FileAccess.Write, FileShare.None))
{
    ftpStream.CopyTo(localFileStream);
}
7
  • Same error. Access denied to path 'C:\Users\Monster\Desktop\ftp'. Unfortunately it did not work.
    – furkank00
    Commented Jul 19, 2022 at 15:47
  • Are you sure your user has access to the path and that no other app has it open? Commented Jul 19, 2022 at 15:48
  • yes, I gave all permissions
    – furkank00
    Commented Jul 20, 2022 at 16:15
  • "I gave all permissions" doesn't fill me with confidence: you shouldn't need to add permissions for your own desktop. Commented Jul 20, 2022 at 16:16
  • I am installing app manifest for Winform and running it for admin. For the file printing process, I right click on the folder where the printing will be done and click on advanced from the security section.
    – furkank00
    Commented Jul 20, 2022 at 16:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.