Results 1 to 3 of 3

Thread: Writing to a text file

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2002
    Location
    Sheffield, United Kingdom
    Posts
    2

    Angry Writing to a text file

    I am trying to write to a text file using VB.NET, I have used the code as explained in a book called "Professional VB.NET" and also on a website. However I still get Permission errors.

    The part of my code that deals with granting the permissions, opening the file for append and then adding to the file is shown below:

    Dim FilePermission As FileIOPermission = New FileIOPermission(FileIOPermissionAccess.AllAccess,
    "C:\Inetpub\wwwroot\temp\pickfile2.lst")
    FilePermission.Assert()
    Try
    Dim writer1 As New StreamWriter(File.Open("C:\Inetpub\wwwroot\temp\pickfile2.lst",
    FileMode.Append))
    writer1.WriteLine(Store.BoxID(Store.BoxNo) & "," & Store.OldLoc(Store.BoxNo)
    & "," & Store.NewLoc(Store.BoxNo))
    writer1.Close()
    Catch objA As System.Exception
    MessageBox.Show(objA.Message)
    End Try


    However when I run this code I get the following error:

    Request for the permission of type System.Security.Permissions.FileIOPermission,
    mscorlib, Version=1.0.3300.0, Culture=netural, PublicKeyToken=b77a5c561934e089
    failed.



    __________________
    David Bradley
    [email protected]

  2. #2
    Registered User jkw119's Avatar
    Join Date
    Oct 2001
    Location
    Pittsburgh
    Posts
    256
    VB Code:
    1. Dim MDocument As New System.IO.FileStream("Sample.txt", IO.FileMode.Create, IO.FileAccess.Write)
    2. Dim w As New System.IO.StreamWriter(MDocument)
    3. w.BaseStream.Seek(0, IO.SeekOrigin.End)
    4. w.WriteLine("Sample Text")
    5. w.WriteLine("Sample Text")
    6. w.Flush()
    7. w.Close()

    This will work...

    Jeff

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2002
    Location
    Sheffield, United Kingdom
    Posts
    2
    Cheers Jeff for that suggestion.

    However, after inserting your suggested code without

    Dim FilePermission As FileIOPermission = New FileIOPermission(FileIOPermissionAccess.AllAccess, "C:\Inetpub\wwwroot\1sStorage_VBNET\PickApp\bin\pickfile3.lst")
    FilePermission.Assert()


    in the code I get the following error:

    An unhandled exception of type 'System.Security.SecurityException' occurred in mscorlib.dll

    Additional information: Request for the permission of type System.Security.Permissions.FileIOPermission, mscorlib, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed.


    With the FilePermission etc in I get the same error as before:

    Request for the permission of type System.Security.Permissions.FileIOPermission,
    mscorlib, Version=1.0.3300.0, Culture=netural, PublicKeyToken=b77a5c561934e089
    failed.


    This is how my code looks now after your suggestion:

    Dim outFile As New System.IO.FileStream("C:\Inetpub\wwwroot\1sStorage_VBNET\PickApp\bin\pickfile3.lst", IO.FileMode.Create, IO.FileAccess.Write)
    Dim writer1 As New System.IO.StreamWriter(outFile)
    writer1.BaseStream.Seek(0, IO.SeekOrigin.End)
    writer1.WriteLine(Store.BoxID(Store.BoxNo) & "," & Store.OldLoc(Store.BoxNo) & "," & Store.NewLoc(Store.BoxNo))
    writer1.Flush()
    writer1.Close()

    Cheers
    David

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width