Outline ·
[ Standard ] ·
Linear+
VB Access to the path C:/Program.. is denied (vb.net), Unable to write into log file sometimes
|
TSragk
|
Aug 20 2011, 09:05 PM, updated 15y ago
|
|
Hi peeps~
i was cleaning a source code (delete unnecessary code) recently, but after i completing clean up the code, some time the log file wont write. it suppose to create a new logfile (logfile_todaydate) everyday. so it will work like this
if logfile_todaydate not existed
create logfile_todaydate write log()
else
write log into logfile_todaydate
endif
the code of writing the logfile in old version and new version is completely the same, but the cleaned up version wont work some time and prompt msg with "Access to the path C:/Program.. is denied", any idea? and im using admin acc and permission to folder is fully granted. And when the error prompted, the previous information in the logfile will be delete. Is suppose to stack with the previous information, somehow its deleted when the error prompt, or b4 the error prompt?
This post has been edited by ragk: Aug 20 2011, 09:06 PM
|
|
|
|
|
|
Eventless
|
Aug 20 2011, 09:16 PM
|
|
So does this error occur when creating a new file, using the old file or both cases? It could be possible that you've already opened the file earlier and have not properly closed it.
If you are losing previously saved data, it is possible that you are opening the file using the wrong mode resulting the the file being overwritten instead of being appended to.
Without seeing the code involved, it is not possible to comment on it.
|
|
|
|
|
|
TSragk
|
Aug 20 2011, 10:26 PM
|
|
Here is my code » Click to show Spoiler - click again to hide... « Try errLog = Application.StartupPath & "\StepLog\"
If Not (Directory.Exists(errLog)) Then Directory.CreateDirectory(errLog) End If
Dim dteToday As Date = Today()
FullPath = errLog & "Step.log." & dteToday
If File.Exists(FullPath) Then
objReader = New StreamReader(FullPath) While objReader.Peek <> -1 TempStr = objReader.ReadLine() i = i + 1 End While objReader.Close() objReader = New StreamReader(FullPath)
Dim LineIn(i - 1) As String j = 0 While objReader.Peek <> -1 LineIn(j) = objReader.ReadLine() j = j + 1 End While
objReader.Close()
File.Delete(FullPath)
objWriter = New StreamWriter(FullPath) Dim x As Integer
For x = 0 To j - 3 objWriter.AutoFlush = True objWriter.WriteLine(Trim(LineIn(x))) Next x objWriter.Write(" ") objWriter.Close()
Else objWriter = New StreamWriter(FullPath)
objWriter.Write(" ") objWriter.Close() End If Catch ex As Exception WriteToLogFile(ex.Message) End Try
End Sub
Added on August 20, 2011, 10:31 pmBecoz all the previous information is deleted, so look like it create a new file and replace it, where it shouldn't, and thn the error prompted. This post has been edited by ragk: Aug 20 2011, 10:31 PM
|
|
|
|
|
|
Eventless
|
Aug 21 2011, 12:51 AM
|
|
Hate to say this but most of your code is not necessary. The part that does the file exist checking, copy , delete and create new file can be removed. Use system.io.file to do the file handling. Use the appendtext method to open the file. This method will create the file if it does not exist and will add to the file when you do a write. The example on the appendtext page should give you the necessary information on how to proceed.
|
|
|
|
|
|
TSragk
|
Aug 22 2011, 01:23 AM
|
|
Is this wat i shud do?
Dim FullPath as string= errLog & "Step.log." & dteToday
Dim objWriter As New System.IO.StreamWriter(FullPath , True)
objWriter.Write(" ")
objReader.Close()
*i dun hav the necessary application to run my project now, so i cant test on it
|
|
|
|
|
|
Eventless
|
Aug 22 2011, 08:40 AM
|
|
As long as it is doing an append instead of an overwrite,it should work.
Why is there a reader in the code? Probably a good idea to make sure nothing else is opening the file when writing to it.
So the only content of the file are spaces(" ")?
|
|
|
|
|
|
TSragk
|
Aug 22 2011, 09:26 AM
|
|
QUOTE(Eventless @ Aug 22 2011, 08:40 AM) As long as it is doing an append instead of an overwrite,it should work. Why is there a reader in the code? Probably a good idea to make sure nothing else is opening the file when writing to it. So the only content of the file are spaces(" ")? oops sorry it shud be objectwriter.close(), the content is not only "", i juz use as example here  Added on August 22, 2011, 11:33 amok after changing the code and tested for few hour now, so far no error occur yet :-) This post has been edited by ragk: Aug 22 2011, 11:33 AM
|
|
|
|
|