File.Delete(): Access to the path 'C:... .dll' is denied

File.Delete(): Access to the path 'C:... .dll' is denied

MiguelGT17
Advocate Advocate
712 Views
2 Replies
Message 1 of 3

File.Delete(): Access to the path 'C:... .dll' is denied

MiguelGT17
Advocate
Advocate

Greetings to everyone,

 

I'm working on an updater tool and so far I managed to pick new files from github and sent them to the app.bundle. The problem I'm facing is that I can not delete the current .dll file in the app.bundle 

 

 

MiguelGT17_0-1668014513186.png

 

even though the file is not blocked or in read-only mode

 

MiguelGT17_1-1668014577863.png

 

Besides that, why is Revit using the dll file when the app bundle loads the addin on start up by default?

MiguelGT17_2-1668014998788.png

 

Any recommendations to fix this?

All the best,

Miguel G.

Accepted solutions (1)
713 Views
2 Replies
Replies (2)
Message 2 of 3

RPTHOMAS108
Mentor
Mentor

That location is protected by the OS with inherited properties. If you have logged in as an administrator, then you can manually delete them by temporarily elevating your privileges (right click delete with shield icon).

 

An installer would request that during installation which is how the files and folders get there.

 

Running things i.e. an add-in with those elevated privileges is probably not a good idea. I don't even know if it is possible for an add-in. I know how to write an executable that requests elevated privileges, but I assume the host to the add-in (Revit) isn't run with them.

 

"Besides that, why is Revit using the dll file when the app bundle loads the addin on start up by default?"

Which alternative were you expecting? That is one of the locations Revit looks in. That aspect is another reason you may not be able to delete it whilst Revit is running.

 

My overall advice is that installers have uninstallers so best to use that or the thing will forever feature in your 'add/remove programs' window. Probably sorting out and testing the contents of the app exchange package is one of the last tasks to do. If your add-in saves files where the dll is located (at runtime), you should think of another location for those items i.e. you have known user folders (special directories) that can be written too instead of where your dll sits.

 

 

Debug.WriteLine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData))
Debug.WriteLine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData))
Debug.WriteLine(commandData.Application.Application.CurrentUsersAddinsDataFolderPath)
Debug.WriteLine(commandData.Application.Application.CurrentUsersDataFolderPath)

C:\Users\<User>\AppData\Roaming
C:\Users\<User>\AppData\Local
C:\Users\<User>\AppData\Roaming\Autodesk\Revit\Autodesk Revit 2023\AddinsData
C:\Users\<User>\AppData\Roaming\Autodesk\Revit\Autodesk Revit 2023

 

 

I can think of an updater that looks for and loads an alternative dll from one of the above locations but that seems like a poor security choice since the folder isn't protected like the ProgramData one is (who knows what the contents could be). I guess in that scenario you could check the certificate of the dll to validate it is your update. However write an msi/exe that updates the contents of ProgramData seems the best simple option since that needs permission.

 

 

 

 

Message 3 of 3

MiguelGT17
Advocate
Advocate
Accepted solution

Thanks for Replying me @RPTHOMAS108 ! I've been working with installers and the team is not satisfied with that as we are fixing bugs quite often and the process of looking for the installer in sharepoint, download it, is time consuming. I like your suggestion of moving the dll file to another folder, I will bear in mind that one for upcoming setups. 

In the end, I came up with this workaround:

 

                            string bundlePath = @"C:\ProgramData\Autodesk\ApplicationPlugins\XXXXXXXX.bundle\Contents\Sources";
                            string dllPathInBundle = bundlePath + @"\XXXXXXXX.dll";


                            string pathToMove = @"C:\ProgramData\Autodesk\ApplicationPlugins\XXXXXXXX.bundle\Contents\XXXXXXXX.dll";
                            if (File.Exists(pathToMove))
                            {
                                File.Delete(pathToMove);//SOMEHOW PREVIOUS DLL FILES I CAN DELETE THEM AS THEY ARE NOT USED BY REVIT
                            }
                            File.Move(dllPathInBundle, pathToMove);

I'm moving the current dll one folder up and sending new dll from github to the original location. For next updates, it'll look for the previous dll that was moved one folder up, if exist, it'll delete it as it's not currently used by revit. 

 

Sometime, Revit will through the Access is denied when trying to delete files and to break the security I manage to get the app.bundle access control (GetAccessControl()), then I modified the access rules for current user (ModifyAccessRule()), finally I set the new access control (SetAccessControl) and now I have full access control to the app folder:

MiguelGT17_0-1668098519314.png

 

All the best,

Miguel G.

 

0 Likes