Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi, i was following the infomation on http://www.xtremevbtalk.com/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

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 doesn't 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

  • Leaders
Posted

Does it work if you just use a StreamReader / StreamWriter?

 

Dim SW As StreamWriter = New StreamWriter(sFilename)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

  • Leaders
Posted

Hmm... what is the application startup path?

 

Does it work if you do a root directory file such as

C:\test.txt

with no directories?

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted
The application path is Z:\Visual Basic 2005\Write to file test\bin\Debug but No it doesent work with a path like C:\test.txt, hmm this is really confusing i downloaded the zip file that was with the tutorial i said i was following above and the write to file function works :confused:
Posted
Are you running the program from a network drive/shared drive/removable drive? .NET won't give those drives/folders permission to do file I/O. See what happens when you save the program to C:\theprogram.exe

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...