Unsaferanger Posted July 11, 2005 Posted July 11, 2005 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 Quote
Leaders Iceplug Posted July 11, 2005 Leaders Posted July 11, 2005 Does it work if you just use a StreamReader / StreamWriter? Dim SW As StreamWriter = New StreamWriter(sFilename) Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
Unsaferanger Posted July 11, 2005 Author Posted July 11, 2005 No it doesent i still get the permition error. Quote
Leaders Iceplug Posted July 12, 2005 Leaders Posted July 12, 2005 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? Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
Unsaferanger Posted July 12, 2005 Author Posted July 12, 2005 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: Quote
thenerd Posted July 12, 2005 Posted July 12, 2005 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 Quote
Unsaferanger Posted July 12, 2005 Author Posted July 12, 2005 Opps, yeah the drive is located at a server in my house, Thanks for pointing it out. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.