error when creating a directory RRS feed

  • Question

  • i have a program which creates a folder in specific location, it worked for weeks and this morning it decided to stop and this is the error -

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

    Additional information: Access to the path "C:\Program Files\fugu creations\save\user.fug" is denied.

    The thing is, it actually creates the directory and the file itself, but will not put the data in it.

    this is my code -

    Public Sub SaveData(ByVal file As String)

    Dim sz As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter

    Dim str As New System.IO.FileStream(file, IO.FileMode.Create)

    Dim myobject As New mySaveObject

    myobject.home1 = home1.text

    myobject.home2 = home2.text

    sz.Serialize(str, myobject)


    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then


    End If

    End Sub

    <Serializable()> _

    Public Class mySaveObject

    Public home1 As String

    Public home2 As String

    Monday, October 16, 2006 9:50 AM

All replies

  • looks fine to me. What happens if you do this in another directory? Does it still throw the error? is "user.fug" a file or a folder? Where does this error happen in the code? when you write to the file?
    Monday, October 16, 2006 11:40 AM
  • it seems to work when it wants to, last night it created the FILE user.fug and saved the relevant information to it, this morning it wont. yet i can save it under any other name... do i have to close the file in someway.
    Monday, October 16, 2006 2:21 PM
  • Are you sure there is not another application, or another part of your own application, which has the file locked? Also, you should add exception handling (Try...Finally) around the code which opens the FileStream to ensure that the file gets closed properly, even if an error occurs.
    Tuesday, October 17, 2006 9:07 PM