I'm building a Setup Project so I can ship my WinUI/WPF application. In said applicaiton I use some NuGet packages that creat files on the executable's location (folder) -- these are for MSAL and WebView2. However, Windows sets only Read access to folders on the Program Files folder, so I wanted to create a custom action that gives write permission to the folder throughout the installation.
Most solutions out there are extremely outdated (.NET framework dependent) or tangents to the overall problem.
I have tried creating a Console Application, writing the following code, shipping it as a DLL/EXE and adding it as a Custom Action. My application installed but still did not have write permissions.
static void Main(string[] args)
{
string directory = args[0];
DirectoryInfo directoryInfo = new DirectoryInfo(directory);
// This gets the "Authenticated Users" group, no matter what it's called
SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null);
// Create the rules
FileSystemAccessRule writerule = new FileSystemAccessRule(sid, FileSystemRights.Write, AccessControlType.Allow);
if (!string.IsNullOrEmpty(directory) && Directory.Exists(directory))
{
// Get your file's ACL
DirectorySecurity fsecurity = FileSystemAclExtensions.GetAccessControl(directoryInfo);
// Add the new rule to the ACL
fsecurity.AddAccessRule(writerule);
// Set the ACL back to the file
FileSystemAclExtensions.SetAccessControl(directoryInfo, fsecurity);
}
}
This code was based on the following thread: https://stackoverflow.com/a/10540927/16751261