Problem reading text file
-
I am trying to read a simple text file using the following code. Dim objReader As StreamReader Dim path As String path = Me.filePath & "app.dat" Try objReader = New StreamReader(path) objReader.ReadToEnd() Me.DBUserID = objReader.ReadLine() Me.DBPassword = objReader.ReadLine() Me.DBServer = objReader.ReadLine() Me.webserviceURL = objReader.ReadLine() objReader.Close() Catch Ex As Exception MessageBox.Show("Cannot Read File:" & Me.filePath & " " & Ex.Message & Ex.StackTrace & Ex.Source) End Try Seems like it should work but i get a nasty exception that I do not understand. "Request for the permission type 'System.Security.Premission.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed. I have permissions to read or write any file I want in this folder. "C:/myprog/app.dat." What am I doing wrong and what do I need to do to fix it. I am using Visual Studio 2005 if that helps.
-
I am trying to read a simple text file using the following code. Dim objReader As StreamReader Dim path As String path = Me.filePath & "app.dat" Try objReader = New StreamReader(path) objReader.ReadToEnd() Me.DBUserID = objReader.ReadLine() Me.DBPassword = objReader.ReadLine() Me.DBServer = objReader.ReadLine() Me.webserviceURL = objReader.ReadLine() objReader.Close() Catch Ex As Exception MessageBox.Show("Cannot Read File:" & Me.filePath & " " & Ex.Message & Ex.StackTrace & Ex.Source) End Try Seems like it should work but i get a nasty exception that I do not understand. "Request for the permission type 'System.Security.Premission.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed. I have permissions to read or write any file I want in this folder. "C:/myprog/app.dat." What am I doing wrong and what do I need to do to fix it. I am using Visual Studio 2005 if that helps.
The error you're posting doesn't have anything to do with the permissions to the folder you have. It has to do with Code Access Security. Basically, the CODE doesn't have the rights to execute file operations, probably because you launched the .EXE from an untrusted network source. Now, you have another problem.
objReader = New StreamReader(path)
objReader.ReadToEnd()
Me.DBUserID = objReader.ReadLine()
Me.DBPassword = objReader.ReadLine()
Me.DBServer = objReader.ReadLine()This code will fail because you opened a file, read it all, then went a tried to read a DBUserID from it. Well, if the file pointer is at the end of the file, how's it supposed to read anything else beyond that? It can't. Remove the
.ReadToEnd()
line. You don't need it for anything.Dave Kreskowiak Microsoft MVP - Visual Basic