Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I know I have encountered and fixed this once before but I can't find the answer

I have a project working perfectly in design mode but after deploying I get an error
System.UnauthorizedAccessException: Access to the path 'C:\Program Files\...\Prison Alarm Manager\Errors\' is denied.

Iknow the error is coming from this code but cna't recall how to fix it.

Dim fs As FileStream = New FileStream(Application.StartupPath & "\Errors\errlog.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)



Full procedure should it help
VB
Public Sub WriteToErrorLog(ByVal msg As String, ByVal stkTrace As String, ByVal title As String)

      'check and make the directory if necessary; this is set to look in the application
      'folder, you may wish to place the error log in another location depending upon the
      'the user's role and write access to different areas of the file system
      If Not System.IO.Directory.Exists(Application.StartupPath & "\Errors\") Then
          System.IO.Directory.CreateDirectory(Application.StartupPath & "\Errors\")
      End If


      'check the file
      Dim fs As FileStream = New FileStream(Application.StartupPath & "\Errors\errlog.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)
      Dim s As StreamWriter = New StreamWriter(fs)
      s.Close()
      fs.Close()

      'log it
      Dim fs1 As FileStream = New FileStream(Application.StartupPath & "\Errors\errlog.txt", FileMode.Append, FileAccess.Write)
      Dim s1 As StreamWriter = New StreamWriter(fs1)
      s1.Write("Title: " & title & vbCrLf)
      s1.Write("Message: " & msg & vbCrLf)
      s1.Write("StackTrace: " & stkTrace & vbCrLf)
      s1.Write("Date/Time: " & DateTime.Now.ToString() & vbCrLf)
      s1.Write("===========================================================================================" & vbCrLf)
      s1.Close()
      fs1.Close()

  End Sub
Posted
Updated 11-May-20 8:49am
v2

You need to give proper security permissions to the folder you are trying to access.

Currently, the file you are trying to write in must be readonly or the ASPNET user must not have the permission. If you are using impersonation then you need to give that user the write priviledge.
:thumbsup:
 
Share this answer
 
Comments
Nish Nishant 11-Jul-10 14:15pm    
Reason for my vote of 5
Worthy of a 5!
Member 14924685 27-Aug-20 16:29pm    
HEEELP TENGO UN PROBLEMA ME INSTALE UNA APP Y ME INFECTE A MI MISMO CON UN VIRUS PARA CONTROLARME Y CUANDO HABRO ESE VIRUS ME SALTA UNA CARPETA DICIENDO ESTO ************** Depuración JIT **** **********
Para habilitar la depuración Just In Time (JIT), el archivo de configuración de esta
aplicación o equipo (machine.config) debe tener el
valor jitDebugging establecido en la sección system.windows.forms.
La aplicación también se debe compilar con la depuración
habilitada

Por ejemplo:

<configuration>
<system.windows.forms jitdebugging = "true">


Cuando esté habilitada la depuración JIT, cualquier excepción no controlada
se enviará al depurador JIT registrado en el equipo
en lugar de controlarlo mediante el cuadro de diálogo.


QUE HAGO COMO SULOCIONO ESTO QUIERO QUE ME APARESCA COMO A ESTA PERSONA

https://www.youtube.com/watch?v=IZ8dNaakb5M

GRACIAS POR SU TIEMPO XD XD
I fixed it by changing the path I save to from ProgramFiles to User/AppData

<pre lang="vb">Dim pAppPath As String
 pAppPath = Application.UserAppDataPath & "\Errors\errlog.txt"


 If Not System.IO.Directory.Exists(Application.UserAppDataPath & "\Errors\") Then
     System.IO.Directory.CreateDirectory(Application.UserAppDataPath & "\Errors\")
 End If

 'check the file
 Dim fs As FileStream = New FileStream(pAppPath, FileMode.Append, FileAccess.Write, FileShare.Write)
 Dim s As StreamWriter = New StreamWriter(fs)
 s.Close()
 fs.Close()

 
Share this answer
 
In my case it was my AVG anti-virus that triggered the exception. I added my VS Projects directory to the "Allowed" list, then had to add the .exe to the exceptions list after I copied the executable to my App directory.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900