-
Sep 6th, 2004, 09:28 PM
#1
Thread Starter
Member
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
-
Sep 6th, 2004, 09:58 PM
#2
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.
-
Sep 6th, 2004, 10:34 PM
#3
Thread Starter
Member
awww shiet.. your right
thanks for the help
-
Sep 6th, 2004, 10:39 PM
#4
Thread Starter
Member
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
-
Sep 6th, 2004, 11:18 PM
#5
Thread Starter
Member
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
-
Sep 7th, 2004, 01:13 AM
#6
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:
Private Function addToLog(ByVal Message As String)
Dim writeText As IO.StreamWriter
Try
writeText = New IO.StreamWriter(New IO.FileStream(errorLogPath, IO.FileMode.Append))
writeText.WriteLine(Message)
Catch ex As Exception
sendMail(EmailOfTech, ex.Message)
MsgBox(ex.Message)
Finally
If Not writeText Is Nothing Then writeText.Close()
End Try
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|