1

I am trying to store the AutoCAD users' settings in a directory under their Program Files. Originally, I had everything stored in the users favorites folder, but I wanted to keep all the plugin documents/files within the same directory. So, I have been trying all kinds of options to get admin rights during runtime, but still have not been successful. This is, more or less, what I just recently tested:

Public Shared Sub GetAdminAccess()
    AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)
    Dim curIdentity As WindowsIdentity = WindowsIdentity.GetCurrent()
    Dim principalPerm As PrincipalPermission = New PrincipalPermission(Nothing, "BUILTIN\Administrators")
    principalPerm.Demand()
End Sub

<PrincipalPermissionAttribute(SecurityAction.Demand, Role:="BUILTIN\Administrators")>
Private Shared Sub CreateSettingsFile()
    Try
        IO.File.Create(SettingsFilePath)
        SettingsFileData = DefaultFileData
        Property_WindowsOnTop = True
        Property_SaveBackups = False
        Property_SkipCreateSite = False
        Property_AlignmentZoomExtents = ToFullExtents
        Property_SaveBackups_Method = _TxtValue_Generic_None
        Property_SaveBackups_MainDirectoryPath = _TxtValue_Generic_None
        Call SetUserSettings(SettingsFileData)
        Call SetPropertySettings()
    Catch IOE As IO.IOException
        MsgBox(IOE.Message)
    Catch Ex As Exception
        MsgBox(Ex.Message)
    End Try
End Sub

Many of my attempts have resulted with this same error:

"Application attempted to perform an operation now allowed by the security policy. To grant this application the required permission, contact your system administrator, or use the Microsoft.NET Framework Configuration tool.

If you click Continue, the application will ignore this error and attempt to continue.

Request for principal permission failed."

John89
  • 45
  • 1
  • 7
  • An app can't write to the Program Files folder without admin rights. Don't do it. If you want a shared, app-spevific location, write to a folder AppData (`System.Environment.SpecialFolder.ApplicationData `) – Flydog57 Jan 21 '21 at 05:15
  • @Flydog57 I went ahead and took your advice. I am very glad I did too because now the Settings.txt is not replaced each time the updated plugin is reinstalled. This is exactly how what I was hoping to achieve from the very beginning. I assume this is specific to that directory? The txt document would always be replaced when stored in any other directory. Anyways, out of curiosity, why shouldn't gaining admin rights to alter a document from within the Program Files directory be practiced? – John89 Jan 21 '21 at 17:44
  • The program files folder (and subfolders under it) are admin protected to prevent programs from replacing installed executable code with a malicious equivalent. If want app-specific data using the appData folder. What I like to do with settings files (particularly if they are per-user) is to use _IsolatedStorage_. I generally keep my settings in an XML file (with an app-specific schema) that I read from Isolated Storage as the app starts up and write back as the app shuts down – Flydog57 Jan 21 '21 at 19:34
  • I added an answer so that your question has a clear answer (i.e., that's the purpose of this site). If it's useful, up-vote it. If it fixed your problem, accept it. – Flydog57 Jan 21 '21 at 23:43
  • Thank you @Flydog57! I tried upvoting it, but I need 15 reputation points for it to be public. However, I will make it clear in this message, I 100% agree and appreciate your help. I am extremely new to programming and never really reached out to anyone like this before so I am very ignorant. I am trying to get into to school to learn as much as I can. I would, ideally, love to know everything there is to know about computers; but I know that is impossible for a single person. If you have any advice that could help educate me on programming, please let me know. – John89 Jan 22 '21 at 18:30

1 Answers1

1

An app can't write to the Program Files folder without administrator rights. The program files folder (and subfolders under it) are admin protected to prevent programs from replacing installed executable code with a malicious equivalent. Just don't do that.

If want app-specific data, use the appData folder. You can access that folder using System.Environment.SpecialFolder.ApplicationData and Environment.GetFolderPath

Get comfortable with the SpecialFolder enumeration (and with System.IO.Path.Combine). They are the correct way to access all of the well-known folders in the Windows OS.

Flydog57
  • 6,851
  • 2
  • 17
  • 18