Unsaferanger
New member
- Joined
- Jul 10, 2005
- Messages
- 4
Hi, i was following the infomation on http://www.computerhelp.forum/showthread.php?t=148864
and i have got my application to open create and read from the file but cant make my application write to a text file and i keep getting this error
and i cant work out whats causing this to happen my full code is below
Thanks Unsafe Ranger
and i have got my application to open create and read from the file but cant make my application write to a text file and i keep getting this error
System.Security.SecurityException: Request for the permission of type System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed.
and i cant work out whats causing this to happen my full code is below
Imports System.IO
Public Class Form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
The variables that we will need
Dim sFileName As String = Application.StartupPath & "\VB.txt"
Dim myFileStream As System.IO.FileStream
Try
We have our variables, lets attempt to open it
myFileStream = New System.IO.FileStream(sFileName, _
FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite)
Catch
Make sure that it exists, and if it doesnt display an error
MessageBox.Show("Count not open the file. Make sure " & sFileName & _
" exists in the location shown.")
Exit Try
Finally
It works, but we are going to try one last method
If myFileStream.CanRead = True Then
This is where we would read from the file
txtFileDisplay.Text = "You have succsessfuly opened/created a file"
We are done with the file now, so close it
myFileStream.Close()
End If
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Declare any variables
Dim sFileName As String = Application.StartupPath & "\VB.txt"
Dim myFileStream As New System.IO.FileStream(sFileName, _
FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read)
Create the StreamReader and associate the filestream with it
Dim myReader As New System.IO.StreamReader(myFileStream)
Read the entire text, and set it to a string
Dim sFileContents As String = myReader.ReadToEnd()
Print it to the textbox
txtFileDisplay.Text = sFileContents
Close everything when you are finished
myReader.Close()
myFileStream.Close()
End Sub
Private Sub btnWriteFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteFile.Click
Declare those variables!
Dim sFileName As String = Application.StartupPath & "\VB.txt"
Dim myFileStream As New System.IO.FileStream(sFileName, _
FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)
Create the stream writer
Dim myWriter As New System.IO.StreamWriter(myFileStream)
Write in what is in the text box
myWriter.WriteLine(txtFileDisplay.Text)
Flush before we close
myWriter.Flush()
Close everything
myWriter.Close()
Clear the text
txtFileDisplay.Text = ""
End Sub
End Class
Thanks Unsafe Ranger