none
Attempted to perform an unauthorized operation.Getting this error when setting up Directory permissions in vb.net RRS feed

  • Question

  • I have a windows from application in which I am trying to write a log file to a particular folder for which only the admin users have access to.I can write the file to the particular folder only if I am running my application as an admin. But now I want to give access for writing the file even if my application is not running in admin mode. But I get an error saying that the while doing that"Attempted to perform an unauthorized operation".  Here is my code. Could some one suggest me a solution.

    Public Sub WriteToFile(ByVal sMessage As String)        Try            Dim FilePath As String = "C:\ProgramData\WriteFileFolder"            Dim UserName As String = System.Environment.UserName         

    Dim DomainName As String = System.Environment.UserDomainName 

    Dim UserAccount As String = String.Format("{0}\{1}", DomainName, UserName)                    Dim dInfo As IO.DirectoryInfo = New IO.DirectoryInfo(FilePath)            Dim dSecurity As DirectorySecurity = dInfo.GetAccessControl()          Dim irUser As IdentityReference = New NTAccount(String.Format("{0}\{1}", DomainName, UserName))   

    Dim objrule As FileSystemAccessRule = New FileSystemAccessRule(irUser, FileSystemRights.FullControl, AccessControlType.Allow)            dSecurity.AddAccessRule(objrule)            dInfo.SetAccessControl(dSecurity)                Dim sb As New StringBuilder()           

    sb.Append("Time:").Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")).Append(Chr(9))           

    sb.Append(sMessage).Append(Chr(9))                     

    Using writer As New StreamWriter(FilePath, True)       

    writer.WriteLine(sb.ToString)               

    writer.Close()            End Using      Catch ex As Exception           

    My.Application.Log.WriteException(ex)      End Try    End Sub






    • Edited by teja06 Thursday, June 13, 2019 5:29 PM
    Thursday, June 13, 2019 5:28 PM

All replies

  • Hello,

    Your best option is to select another folder that does not require special permissions in this case. That will save you lots of undue trouble otherwise.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, June 13, 2019 6:12 PM
    Moderator
  • It is likely that when you created the C:\ProgramData\WriteFileFolder you did so when running as an Administrator.

    Consequently, any changes to the security descriptor for this folder must be made by an Administrator.  This would be the reason for the "unauthorized operation" exception

    Had you created the WriteFileFolder folder when running as a standard user then the account used to create the folder would have sufficient permissions to change the security descriptor.

    And if you only want to grant read/write access, then using Full Control is excessive.


    • Edited by RLWA32 Thursday, June 13, 2019 11:21 PM
    Thursday, June 13, 2019 11:18 PM