-2

First of all, the forum has given me very good ideas and solutions just by reading along.

To my current case I willnot really looking for it.

I have written a WPF application that works perfectly in debug and release mode. However, as soon as I publish you, no more.

I tried different ways to publish.

The application is provided by CD, DVD or stick. The clickonce method was once used sometimes not.

It should be created as independently executable. And the framework is included.

My problem is that after publishing via the release build, I have no permission to write to files in Environment.CurrentDirectory or outside.

In one case, the affected file is supplied but cannot be changed. As soon as I exchange the file after installation, I can change it.

In another case, I can't create a file using the directive. The StreamWriter is used. But also creating folders using System.IO is not possible.

The application is run by an administrator. Only when the application is executed with RunAsAdmin does it run without problems.

The file is a txt file that acts as a log file. However, users without administrator rights should also be able to update the log file if necessary.

What can I do so that my application can generally generate files, regardless of the user rights?

Thank you very much.

Trat
  • 1
  • What security parameters does the parent folder has? Does it allow users to edit the content of the folder or it is only allow the administrator? (R-click->properties->security tab) – turanszkik Sep 16 '21 at 07:30
  • 1
    Programs cannot write to the program files directory. If your program is installed there you will get the problems you describe. Solve by writing to the appdata directory instead. – JonasH Sep 16 '21 at 07:38
  • The parent folder has a share for all users who should use the application. Also the admin has the release and full access. -The application is not installed in a special folder like Program files (x64/x84) but in my setup project. Since I have set the special Folder Program File (x84) as placeholder path. – Trat Sep 16 '21 at 07:48

1 Answers1

0

You can check my code snippet:

private bool HasPathAccessable(string path)
{
    DirectorySecurity sec = Directory.GetAccessControl(path);
    WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
    foreach (FileSystemAccessRule acr in sec.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)))
    {
        //Console.WriteLine("{0} | {1} | {2} | {3} | {4}", acr.IdentityReference.Value, acr.FileSystemRights, acr.InheritanceFlags, acr.PropagationFlags, acr.AccessControlType);
        if (acr.IdentityReference.Value == currentIdentity.Name)
        {
            if (acr.FileSystemRights == FileSystemRights.FullControl && acr.AccessControlType == AccessControlType.Allow)
            {
                Console.WriteLine($"{currentIdentity.Name} accessable to {path}");
                return true;
            }
        }
    }
    Console.WriteLine($"{currentIdentity.Name} inaccessable to {path}");
    return false;
}

private void ChangePathAccessControlCurrentUser(string path, bool isFile)
{
    WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();

    if (isFile)
    {
        var pathAccessControl = File.GetAccessControl(path);
        pathAccessControl.AddAccessRule(new FileSystemAccessRule(currentIdentity.Name, FileSystemRights.FullControl, AccessControlType.Allow));
        File.SetAccessControl(path, pathAccessControl);
        Console.WriteLine($"AddAccessRule File : {path}");
    }
    else
    {
        var pathAccessControl = Directory.GetAccessControl(path);
        pathAccessControl.AddAccessRule(new FileSystemAccessRule(currentIdentity.Name, FileSystemRights.FullControl, AccessControlType.Allow));
        Directory.SetAccessControl(path, pathAccessControl);
        Console.WriteLine($"AddAccessRule Directory : {path}");
    }
}
Jonio
  • 1,213
  • 2
  • 15
  • 35
benny
  • 1
  • 2
  • Thank you Benny, your code looks good. I try it and give you an answer. – Trat Sep 16 '21 at 08:00
  • 5
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 16 '21 at 08:48