Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i just have went from VB 2008 to VB 2022 what a problem for me, but here is my question...

i converted some code and got this error

System.UnauthorizedAccessException: 'Access to the path 'C:\1_temp\Testfile.rtf' is denied.'

my test code is as follows... the first thing i do is create a file in the folder, so i know every thing is working, I load the list box, then save it, that is when i get the error, and nothing is saved

Public Class Form1
    Dim RichTextFile As String = "C:\1_temp\Testfile.rtf"

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        'Dim file As System.IO.FileStream
        Dim File = System.IO.File.Create("C:\1_temp\Testfile.txt")

        ' load richtextbox
        RichTextBox.LoadFile(RichTextFile)

        On Error Resume Next
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        RichTextBox.SaveFile(RichTextFile, RichTextBoxStreamType.RichText)
        On Error Resume Next
        End
    End Sub



End Class


What I have tried:

thanks for the help with this problem
Posted
Updated 20-Feb-22 12:01pm
Comments
Andre Oosthuizen 21-Feb-22 4:39am    
As per below comments, run VS as administrator OR set it to run as admin every time you start it.

This issue is associated with directory permission or file is read-only:
1. Run your visual studio, run as Administrator because sometimes caller does not have the required permission to run the code.
2. Path specified [C:\1_temp\Testfile.rtf] a read-only file. Check the file accessibility attributes.
 
Share this answer
 
Comments
bugsxx 20-Feb-22 17:14pm    
1) i am the only user and assume that when i open studio i am the Administrator
2) the file is not read-only, but the folder is and have try everything to change it to uncheck the read only property
Read the error message, it's pretty clear:
Access to the path 'C:\1_temp\Testfile.rtf' is denied.

The file is not available because it's already been opened in the method. When you use File.Create, it creates or empties a file, opens it for writing, and returns the stream.

If you try to follow that with a second access, it will fail, because the file is locked to the first stream until that is closed.

And any code which includes On Error Resume Next should be taken outside and shot ...
 
Share this answer
 
Comments
bugsxx 20-Feb-22 17:16pm    
the file.create is only there to show that the folder is not write protected

the txt file is created once and the rft file is loaded once, the when it is saved, i get the error

thanks for the help
Quote:
System.UnauthorizedAccessException: 'Access to the path 'C:\1_temp\Testfile.rtf' is denied.'

Did you created the file ?
Your create a .txt and then try to use a .rtf file. Is it correct ?

Did you make sure the file is not already use ?
 
Share this answer
 
v2
Comments
bugsxx 20-Feb-22 18:31pm    
i created the .txt file to show that the folder was not "read only", then i downed the info to the ListBox, then i trayed to write the info back to the same file...... that is when i get the error code... denied access

thanks 4 the help

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