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

  • Thread starter Thread starter Basilis1985
  • Start date Start date
B

Basilis1985

Guest
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

Continue reading...
 
Back
Top