Late bound calls to file system methods in the Visual Basic runtime are not permitted.

KarlTheHopeless
KarlTheHopeless used Ask the Experts™
on

Can someone please explain why this simple routine fails? ...

..............

Dim tem As String = ""
        tempno = FreeFile()
       
        FileOpen(tempno, "c:\myvbastext\listoffilesandpaths.txt", OpenMode.Input)
        i% = 0
        While Not EOF(tempno)
            i% = i% + 1
            Input(tempno, tem)

                   End While

............

The error message is "Late bound calls to file system methods in the Visual Basic runtime are not permitted." and the error is described as a "SecurityException was unhandled."

Thank you.

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Adam MenkesC# ASP.NET Developer
Top Expert 2010

Commented:
you might just check to see if the file and path exists. I modified the code and it works on my system.

Author

Commented:
@amenkes

Yes, it is definitely there.

And I cannot open any of my text files using the above code.

I am wondering if it is something to do with file access permissions.

I am writing a program just for my own use, and I want to be able to access all my files on my computer.

Author

Commented:
I have just discovered that the FileOpen method requires the File Access Permissions to be set.

But I cannot seem to set them.

How, exactly, would I set the above program to have access to all my files?
C# ASP.NET Developer
Top Expert 2010
Commented:
Try this and see if it works instead of the FileOpen
Dim i As Integer
        Dim currentRow As String

        Using myreader As New FileIO.TextFieldParser("c:\temp\adam.txt")
            While Not myreader.EndOfData
                currentRow = myreader.ReadLine()
                i = i + 1
            End While
        End Using

Open in new window

Author

Commented:
@amenkes

IT WORKED!

So, thank you.

But the very fact that  it worked has got me very wound up, because the VB Error Message kept telling me that I had a File Permission problem and/or a 'Full Trust' problem; and I have spent ages going down that track -  and getting nowhere.

And I have just discovered that the INPUT method used in my code above was wrong - and that I should have used the LINEINPUT method.

In other words, the VB Error Message was totally inappropriate and misleading.

Ho Hum. Only about six hours wasted.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial