Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Problem reading text file

Problem reading text file

Scheduled Pinned Locked Moved Visual Basic
helpcsharpvisual-studiosecurityannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jawa2006
    wrote on last edited by
    #1

    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.

    D 1 Reply Last reply
    0
    • J Jawa2006

      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.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups