none
How to fix System.UnauthorizedAccessException when i'm trying to copy a file from a server to a local path RRS feed

  • Question

  • Hello all

    I'm new at vb programming and i would like your help the solve the below issue.

    With the code that shown below i'm trying to download a .cat file from a ftp server and copy this file to a folder at my computer.

    The problem is that i'm getting an Unauthorized.AccessException for this folder at the bold line.

    I've tried manually to change the permissions for this folder and with the below code but i'm still getting the same error. 

    

    Private Sub UpdateBtn_Click(sender As Object, e As EventArgs) Handles UpdateBtn.Click Dim Dinfo As New DirectoryInfo(LocalPath_Cat1) Dim DSecurity As DirectorySecurity = Dinfo.GetAccessControl DSecurity.AddAccessRule(New FileSystemAccessRule("xytas", FileSystemRights.FullControl,InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow)) DSecurity.AddAccessRule(New FileSystemAccessRule("xytas",FileSystemRights.FullControl, InheritanceFlags.ObjectInherit,PropagationFlags.InheritOnly, AccessControlType.Allow)) Dinfo.SetAccessControl(DSecurity) If Cat1_StatusLbl.Text = "Outdated!!!" And Directory.Exists(LocalPath_Cat1) And LocalPath_Cat1 <> ""

    Then Dim request As FtpWebRequest = WebRequest.Create(ftp_Cat1_Adress) request.Method = WebRequestMethods.Ftp.DownloadFile 'System.IO.File.SetAttributes(LocalPath_Cat1, IO.FileAttributes.Normal) Using ftpStream As Stream = request.GetResponse().GetResponseStream(), fileStream As Stream = File.Create(LocalPath_Cat1) ftpStream.CopyTo(fileStream) End Using End If End Sub


    Wednesday, August 21, 2019 8:46 AM

All replies

  • Hi,

    You can try my method to download a ftp server file to a local path.

    Sub getFileFromFTP(ByVal localFile As String, ByVal remoteFile As String, ByVal host As String, ByVal username As String, ByVal password As String)
            Dim URI As String = host & remoteFile
            Dim ftp As System.Net.FtpWebRequest = CType(FtpWebRequest.Create(URI), FtpWebRequest)
            ftp.Credentials = New System.Net.NetworkCredential(username, password)
            ftp.KeepAlive = False
            ftp.UseBinary = False
            ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
            Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse, System.Net.FtpWebResponse)
                Using responseStream As IO.Stream = response.GetResponseStream
                    Using fs As New IO.FileStream(localFile, IO.FileMode.Create)
                        Dim buffer(2047) As Byte
                        Dim read As Integer = 0
                        Do
                            read = responseStream.Read(buffer, 0, buffer.Length)
                            fs.Write(buffer, 0, read)
                        Loop Until read = 0 'see Note(1)
                        responseStream.Close()
                        fs.Flush()
                        fs.Close()
                    End Using
                    responseStream.Close()
                End Using
                response.Close()
            End Using
        End Sub

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 22, 2019 2:05 AM
    Moderator