none
Unauthorized Access exception RRS feed

  • Question

  • I'm Working on a program and I keep running into "Unauthorized Access exception" error and cant seem to find out how to fix it.

    Tuesday, May 22, 2018 1:33 AM

All replies

  • this is the code 
    Imports System.Security.Permissions
    Imports System.IO
    Imports System.IO.FileStream
    Public Class MovieCollection
        Structure VidCollection
            Dim nameOfvideo As String
            Dim yearproduced As Integer
            Dim runTime As String
            Dim rating As String
        End Structure
        Dim mytextfile As StreamWriter
        Dim mysearchfile As StreamReader
    
        Private Sub mnuFileSave_Click(sender As Object, e As EventArgs) Handles mnuFileSave.Click
            Dim vidRecord As VidCollection
            vidRecord.nameOfvideo = txtVideoName.Text
            vidRecord.yearproduced = Convert.ToInt32(txtYearProduced.Text)
            vidRecord.runTime = txtRunTime.Text
            vidRecord.rating = txtRating.Text
            mytextfile = File.CreateText("D:\VideoCollection.txt")
            mytextfile.WriteLine(vidRecord.nameOfvideo)
            mytextfile.WriteLine(vidRecord.yearproduced)
            mytextfile.WriteLine(vidRecord.runTime)
            mytextfile.WriteLine(vidRecord.rating)
            mytextfile.Close()
            Reset()
        End Sub
    
        Private Sub mnuReportView_Click(sender As Object, e As EventArgs) Handles mnuReportView.Click
            Dim movieReport As String
            movieReport = "Report of Movie Collection" + vbNewLine
            mysearchfile = File.OpenText("D:\VideoCollection.txt")
            Try
                While Not mysearchfile.EndOfStream
                    movieReport += mysearchfile.ReadLine() + " "
                    movieReport += mysearchfile.ReadLine() + " "
                    movieReport += mysearchfile.ReadLine() + " "
                    movieReport += mysearchfile.ReadLine() + " "
                    movieReport += vbNewLine
                End While
            Catch ex As Exception
            End Try
            MessageBox.Show(movieReport)
        End Sub
    
        Private Sub mnuSearch_Click(sender As Object, e As EventArgs) Handles mnuSearch.Click
            mysearchfile = File.OpenText("D:\VideoCollection.txt")
            Dim saveName As String
            Dim flagValue As Integer
            flagValue = 0
            saveName = InputBox("Enter Movie Name")
            Dim movieSearchRecord As VidCollection
            Try
                While Not mysearchfile.EndOfStream
                    movieSearchRecord.nameOfvideo = mysearchfile.ReadLine()
                    movieSearchRecord.yearproduced = CInt(mysearchfile.ReadLine())
                    movieSearchRecord.runTime = mysearchfile.ReadLine()
                    movieSearchRecord.rating = mysearchfile.ReadLine()
                    If movieSearchRecord.nameOfvideo.Equals(saveName) Then
                        flagValue = 1
                        Exit While
                    End If
                End While
                If flagValue.Equals(1) Then
                    txtVideoName.Text = movieSearchRecord.nameOfvideo.ToString
                    txtYearProduced.Text = movieSearchRecord.yearproduced.ToString
                    txtRunTime.Text = movieSearchRecord.runTime.ToString
                    txtRating.Text = movieSearchRecord.rating.ToString
                Else
                    MessageBox.Show("Records do not exist")
                    Reset()
                End If
            Catch ex As Exception
    
            End Try
        End Sub
    
        Private Sub mnuClose_Click(sender As Object, e As EventArgs) Handles mnuClose.Click
            Me.Close()
        End Sub
        Public Sub reset()
            txtVideoName.Text = ""
            txtYearProduced.Text = ""
            txtRunTime.Text = ""
            txtRating.Text = ""
        End Sub
    End Class
    

    Tuesday, May 22, 2018 1:37 AM
  • Maybe you really do not have access to the root folder of disk D:. Try another folder. For example, you can create the file on Deskt op:

    Dim mytextfile = File.CreateText(My.Computer.FileSystem.SpecialDirectories.Desktop & "\VideoCollection.txt")

    This file will appear on the screen.

    In addition, make sure the file is closed using mysearchfile.Close().




    • Edited by Viorel_MVP Tuesday, May 22, 2018 6:05 PM
    Tuesday, May 22, 2018 5:26 AM
  • Which line of code causes the exception, and what actions do you have to take to cause it?  I'm guessing it is as Viorel_ says and you are locking yourself out of your own file, but we need to know where the exception occurs and what steps it takes to reproduce it.  Not closing the file is the primary culprit at this point.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, May 22, 2018 11:57 AM
    Moderator
  • If D is the driveletter of your boot drive, then the root folder is not allowed to write to for a program. 

    Success
    Cor

    Tuesday, May 22, 2018 7:14 PM