Results 1 to 6 of 6

Thread: using StreamWriter and it being access denied?

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2004
    Posts
    59

    using StreamWriter and it being access denied?

    I am trying to create an error log, so i created a function called "addToLog" in which it writes info into a file:

    Public Function AddtoLog(ByVal Message As String)
    Dim writeText As StreamWriter

    If fileExists(errorLogPath) Then
    Try
    writeText = New StreamWriter(File.OpenWrite(errorLogPath))
    Catch ex As Exception
    sendMail(EmailOfTech, ex.Message)
    MsgBox(ex.Message)
    End Try

    Else
    Try
    writeText = New StreamWriter(File.Create(errorLogPath))
    Catch ex As Exception
    sendMail(EmailOfTech, ex.Message)
    MsgBox(ex.Message)
    End Try
    End If

    writeText.WriteLine(Message)

    end Function



    The problem is that when i try to create a file, it says access is denied. I am running it on my local machine, and i could create a file regularly, so i don't know why it would permit me to create a file on my own system.

    Thanks

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You never close the stream so after the first call it gets locked and can not be reopened.

    Just call the close method of the stream after writing to it.

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2004
    Posts
    59
    awww shiet.. your right

    thanks for the help

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2004
    Posts
    59
    oh wait... still no luck =(

    Still doesn't work

    code:

    Private Function addToLog(ByVal Message As String)

    Dim writeText As StreamWriter

    If fileExists(errorLogPath) Then
    Try
    writeText = New StreamWriter(File.OpenWrite(errorLogPath))
    Catch ex As Exception
    sendMail(EmailOfTech, ex.Message)
    MsgBox(ex.Message)
    End Try

    Else
    Try
    writeText = New StreamWriter(File.Create(errorLogPath))
    Catch ex As Exception
    sendMail(EmailOfTech, ex.Message)
    MsgBox(ex.Message)
    End Try
    End If

    writeText.WriteLine(Message)

    writeText.Close()
    writeText = Nothing


    End Function

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2004
    Posts
    59
    could this be a problem with the .net framework security policy?

    I am desiging with win2003 adv. server if that makes any different and visual studio .net

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I don't understand your use of the Try blocks since if an error occurs then it will not write to the log but will still leave it open.

    Try something like this:
    VB Code:
    1. Private Function addToLog(ByVal Message As String)
    2.         Dim writeText As IO.StreamWriter
    3.  
    4.         Try
    5.             writeText = New IO.StreamWriter(New IO.FileStream(errorLogPath, IO.FileMode.Append))
    6.             writeText.WriteLine(Message)
    7.         Catch ex As Exception
    8.             sendMail(EmailOfTech, ex.Message)
    9.             MsgBox(ex.Message)
    10.         Finally
    11.             If Not writeText Is Nothing Then writeText.Close()
    12.  
    13.         End Try
    14.  
    15.     End Function

    Note: Append will create the file if it does not exist otherwise it uses the one that exists and moves to the end of the stream.

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