Results 1 to 4 of 4

Thread: System.UnauthorizedAccessException (Access to the path denied) Error

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2016
    Posts
    9

    System.UnauthorizedAccessException (Access to the path denied) Error

    Hello,

    I'm building an application that copies/downloads new txt file from a mapped network drive (sFTP site) to a local folder triggered by timer.

    The application will run fine for hours and randomly gets an error message: "Access to the path 'G:\SourcePath' is denied"

    Error message:
    Code:
    ************** Exception Text **************
    System.UnauthorizedAccessException: Access to the path 'G:\SourcePath' is denied.
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.FileSystemEnumerableIterator`1.CommonInit()
       at System.IO.FileSystemEnumerableIterator`1..ctor(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler, Boolean checkHost)
       at System.IO.Directory.GetFiles(String path, String searchPattern, SearchOption searchOption)
       at Test1.Form1.CopyFiles()
       at Test1.Form1.tmrCountdown_Tick(Object sender, EventArgs e)
       at System.Windows.Forms.Timer.OnTick(EventArgs e)
       at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    I'm having trouble figuring this out why it keeps on happening..

    Here is my code:

    Code:
    Private Sub CopyFiles()
    
            DISPLAYINFO("Checking for new files to process...")
    
            Dim sourcePath As String
            Dim destPath As String
    
            sourcePath = "G:\SourcePath\"
            destPath = "C:\test\DestinationPath\"
    
            Dim files() As String = Directory.GetFiles(sourcePath, "*.TXT", SearchOption.TopDirectoryOnly)
            Dim x As Integer
    
                If files.Count > 2 Then
                    Debug.Print(files.Count)
                End If
    
                For x = 0 To files.Count - 1
    
                    Dim sourceFile As String = Nothing
                    Dim destFile As String = Nothing
    
                    sourceFile = files(x)
                    destFile = destPath & Path.GetFileName(sourceFile)
    
                    strFileName.Text = Path.GetFileName(sourceFile)
                           
                        tmrCountdown.Enabled = False
    
                        If File.Exists(destFile) = True Then
                            DISPLAYINFO(Path.GetFileName(sourceFile) & " already exists.. Skipping")
                                                  GoTo NextFile
                        Else
                            Try
                            File.Copy(sourceFile, destFile, True)
                            File.SetAttributes(destFile, FileAttributes.Normal)
    
                            DISPLAYINFO(Path.GetFileName(sourceFile) & " download complete..")
      
                            Catch ex As Exception
                                DISPLAYINFO("Failed to copy file from:" & vbCrLf & sourceFile & vbCrLf & "to" & vbCrLf & destFile)                            
                                GoTo NextFile
                            End Try
    
                        End If
          
    NextFile:
                Next
    			
    			tmrCountdown.Enabled = True
        End Sub
    Here is the timer code:
    Code:
     Private Sub tmrCountdown_Tick(sender As Object, e As EventArgs) Handles tmrCountdown.Tick
    
            Dim GDrive As String = "G:\SourcePath\"
    
            If Directory.Exists(GDrive) Then
                CopyFiles()
            Else
                DISPLAYINFO("Network drive disconnected...")
            End If
    
        End Sub
    TIA for looking and for your help!
    Last edited by web2k6; Apr 16th, 2021 at 01:44 PM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    Re: System.UnauthorizedAccessException (Access to the path denied) Error

    If you were to leave it running, will it then start working again, or is it the case that once it fails it always fails?

    The reason I ask is because there are a variety of network things that could cause this, so rather than preventing it, you might want to think about how to recover from it. If you simply trap the exception and either log it, or even ignore it, if the program then starts working again, that may be good enough. After all, even if you identify this particular problem, others network issues could arise that would result in the exact same exception for a different reason. So, perhaps noting that it is down is enough, or logging failures, or something like that.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2016
    Posts
    9

    Re: System.UnauthorizedAccessException (Access to the path denied) Error

    Hi Shaggy thanks for your input!

    It will still keep on running. It bugs the heck out of me when leaving it overnight and checking in the morning with the big annoying error message box on your screen.

    Is there a way to disable that? I tried doing regedits and jit settings on debug options but it doesn't seem to work.

    Do you have any suggestions on what & where to write the code for the issues you've mentioned or how to trap the exception? I'm still a noob code writer.

    As I've tried to check if the mapped network drive exists logic but it doesn't seem to be the issue.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    Re: System.UnauthorizedAccessException (Access to the path denied) Error

    It may be enough to wrap your call to CopyFiles in an exception handler.

    The exception seems likely to be thrown by this line:

    Dim files() As String = Directory.GetFiles(sourcePath, "*.TXT", SearchOption.TopDirectoryOnly)


    if the path isn't accessible. You might wrap just that line in an exception handler, but there appear to be other things in CopyFiles. What you don't want is to trap the error in one place only to have it move. It seems like all of Copy Files can succeed or fail, so wrapping that call in an exception handler might be the best and easiest solution.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width