Share/Bookmark

Featured Posts

Video Game Website Gives all it's profit to Charity Sometimes you read an article that just makes you smile, this is such one: http://www.ps3attitude.com/new/2010/02/no-fuss-reviews-4000-charity/ A video game review website that has given all of it's...

Read more

SQL AD Group membership Hi all, I have been working on a way to return Active directory group membership from a linked AD server in SQL. Its quite hard as you can`t return it from the group - you have to return it from...

Read more

Face Detection with PHP A very useful piece of PHP code that will highlight any pictures where a face has been detected. Heres the first piece of PHP code you will need, it would be best to make this an include: [sourcecode...

Read more

Basic Guide to Database Normalisation: First Normal... Before I start talking about the normal forms, I thought it would be best to explain what I am planning to write about, and what normalisation is. I am going to cover what normalisation is for beginners...

Read more

Sony: Synonymous with Innovation Sony have always been on the leading edge of innovation. From creating the WalkMan back in the 80's to popularising the CD, DVD and Blu-Ray with their PlayStation products. In this article we're discussing...

Read more

NTFS File Owner in VB.net

Posted by daniel | Posted in Code Snippets | Posted on 26-11-2009

Tags: , ,

0

Here is some VB.net 2005 code to set an NTFS file\folder owner:

For both of these you need to add:

Imports System.Security.AccessControl
Imports System.Security.Principal

Private Sub setOwner(ByRef FileName As String, ByRef account As NTAccount)
Try
Dim dInfo As New DirectoryInfo(FileName)
Dim dir As New DirectorySecurity(FileName, AccessControlSections.None)

End Sub

dir.SetOwner(account)
dInfo.SetAccessControl(dir)

dir = Nothing
dInfo = Nothing
If System.IO.Directory.Exists(FileName) Then
Dim Files As String()
Files = Directory.GetFileSystemEntries(FileName)

For Each element As String In Files
setOwner(element, account)
Next
End If

Catch ex As Exception
Throw New Exception(ex.Message.ToString())
End Try
End Function

Here is how to get an owner:

Private Function GetOwner(ByVal Path As String) As String
Try
Dim User As System.Security.Principal.NTAccount = DirectCast( _
System.IO.File.GetAccessControl(Path). _
GetOwner(GetType( _
System.Security.Principal.NTAccount)), _
System.Security.Principal.NTAccount)
Return User.Value
End Function

My next post will be looking at viewing\setting \removing NTFS permissions…. so keep an eye out!

Write a comment