T
teja06
Guest
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
Continue reading...
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
Continue reading...