Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Option Explicit On
Imports Microsoft.VisualBasic.FileSystem
Module Module1
    Structure ConcertTour
        Dim BandName As String
        Dim noofGigs As Integer
        Dim StartDate As Date
    End Structure
    Dim MyTour As ConcertTour

    Public Sub Main()
        Dim choice As Integer
        FileOpen(1, "C:\ConcertsRandom.DAT", OpenMode.Random)
        Do
            Console.WriteLine(" 1 to create. 2 to Enter. 3 to retrieve.")
            Console.Write("choice? ") : choice = Console.ReadLine
            If choice = 1 Then Call CreateDummyRecords()
            If choice = 2 Then Call EnterRecords()
            If choice = 3 Then Call RetrieveRecords()

        Loop Until choice = 4
        FileClose(1)
        Console.ReadLine()


    End Sub
    Sub CreateDummyRecords()
        Dim i As Integer
        For i = 1 To 30
            MyTour.BandName = "********************"
            MyTour.noofGigs = 0
            MyTour.StartDate = #1/1/2016 12:00:00 PM#
            FilePut(1, MyTour, i)
        Next
        Console.WriteLine()
        Console.WriteLine("dummyfiles created...")
    End Sub

    Sub EnterRecords()
        Dim hashcode As Integer
        Do
            Console.Write(" Band name (xxx to end)? ")
            MyTour.BandName = Console.ReadLine
            If MyTour.BandName <> "xxx" Then
                Console.Write(" no of gigs: ") : MyTour.noofGigs = Console.ReadLine
                Console.Write("start date: ") : MyTour.StartDate = Console.ReadLine
                hashcode = Asc(Left(MyTour.BandName, 1)) - 64
                FilePut(1, MyTour, hashcode)
            End If
        Loop Until MyTour.BandName = "xxx"
    End Sub

    Sub RetrieveRecords()
        Dim hashcode As Integer
        Console.Write(" Band name? ")
        MyTour.BandName = Console.ReadLine
        hashcode = Asc(Left(MyTour.BandName, 1)) - 64
        FilePut(1, MyTour, hashcode)
        Call DisplayRecordData(hashcode)
    End Sub

    Sub DisplayRecordData(ByVal thishashcode As Integer)
        Console.Write(" record key " + Str(thishashcode) + " ")
        Console.Write(MyTour.BandName + " ")
        Console.Write(Str(MyTour.noofGigs) + " ")
        Console.WriteLine(MyTour.StartDate)
    End Sub





End Module


What I have tried:

i have tried changing the file path and with different imports system it does not work.
Posted
Updated 10-Jun-18 21:57pm
Comments
CHill60 11-Jun-18 3:18am    
You will almost certainly get this error if you attempt to use the root of the C: drive. Also if you use anything under C:\Program Files (x86) or C:\Program Files. What did you change the file path to?

1 solution

The error message is pretty clear: UnauthorizedAccessExceotion means what is says - yo9u tried to access a file or directory that the user your code is running as does not have the right permissions to access in that way: it may be that you don't have Write access to the file, or you may not have any access to the folder. We can't tell.

But ... if that is really your path: C:\ConcertsRandom.DAT then you will not have write access to it except as an admin as it is your boot drive and the root folder is write protected to prevent virus activity messing with system files.
Do not store data in the root of any drive, and especially not in teh root of a bootable drive.

See here: Where should I store my data?[^] - the code is in C# but it's pretty obvious.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900