Windows Forms Application Error: Attempted to perform an unauthorized operation

Last Reply 10 months ago By kalpesh

Posted 10 months ago

I'm trying to write log file once application(winform) start and some oprations going on at background.

It's working fine with Visual Studio, but once I create exe file and run it... it gives an error of attempted to perform an unauthorized operation c# winform log file

Is there any other way arround to give access of instattion folder once application installed to the user's machine?

Here is my code:

public SplashScreen()
{
    InitializeComponent();
    GrantAccess();
}

private void GrantAccess()
{
    try
    {
        var path = Path.GetDirectoryName(Application.StartupPath);

        DirectoryInfo dInfo = new DirectoryInfo(path);
        DirectorySecurity dSecurity = dInfo.GetAccessControl();
        dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
        dInfo.SetAccessControl(dSecurity);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
        Console.ReadLine();
    }
}