DESKTOP

Get File Security Description

9/25/2010 3:14:37 PM
Problem : You want to retrieve security information for a file. Security descriptors can be attached to many types of objects, but some of the most common to deal with are files.

Solution:
The System.Security namespace contains .NET classes related to security. The following code sample display security information about a file whose name is passed on the command line.

using System;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;

namespace ShowFileSecurity
{
class Program
{
static void Main(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Usage: ShowFileSecurity filename");
return;
}
string filename = args[0];
FileInfo info = new FileInfo(filename);
FileSecurity security = info.GetAccessControl();
ShowSecurity(security);
}

private static void ShowSecurity(FileSecurity security)
{
AuthorizationRuleCollection coll =
security.GetAccessRules(true, true, typeof(NTAccount));
foreach (FileSystemAccessRule rule in coll)
{
Console.WriteLine("IdentityReference: {0}",
rule.IdentityReference);
Console.WriteLine("Access control type: {0}",
rule.AccessControlType);
Console.WriteLine("Rights: {0}", rule.FileSystemRights);
Console.WriteLine("Inherited? {0}", rule.IsInherited);

Console.WriteLine();
}
}
}
}


You can run the program from the command line like this:

D:\code\ch11\ShowFileSecurity\bin\Debug>ShowFileSecurity.exe ShowFileSecurity.pdb

It produces output similar to the following:
IdentityReference: Ben-Desktop\Ben
Access control type: Allow
Rights: FullControl
Inherited? True

IdentityReference: NT AUTHORITY\SYSTEM
Access control type: Allow
Rights: FullControl
Inherited? True
IdentityReference: BUILTIN\Administrators
Access control type: Allow
Rights: FullControl
Inherited? True


Other  
  •  Combine Streams (Compress a File)
  •  Create, Read, and Write a Binary File
  •  Create, Read, and Write a Text File
  •  Opening a Local File from a Silverlight Application
  •  Navigating Architecture Changes in Windows Vista
  •  Getting Around Windows Vista
  •  Managing User Account Control and Elevation Prompts
  •  Supporting Computers Running Windows Vista
  •  Using System Support Tools in Vista
  •  Managing System Properties
  •  Introducing Automated Help And Support in Vista
  •  Working with the Automated Help System
  •  Installing and Maintaining Devices in Vista: The Essentials
  •  Getting Started with Device Manager
  •  Working with Device in Vista
  •  Managing Hardware in Vista
  •  Customizing Hardware Device Settings
  •  Managing Internet Time in Vista
  •  Optimizing Windows Vista Menus
  •  Customizing the Taskbar in Vista
  •  
    Top 10
    -  HP Spectre XT Touchsmart Review - Everything Is Fine Except Battery Life (Part 2)
    -  HP Spectre XT Touchsmart Review - Everything Is Fine Except Battery Life (Part 1)
    -  Lenovo Thinkpad Carbon Touch Ultrabook Review (Part 3)
    -  Lenovo Thinkpad Carbon Touch Ultrabook Review (Part 2)
    -  Lenovo Thinkpad Carbon Touch Ultrabook Review (Part 1)
    -  Blackberry 10 OS (Part 3)
    -  Blackberry 10 OS (Part 2)
    -  Blackberry 10 OS (Part 1)
    -  How To Keep High-End Speakers In Good Conditions
    -  How To Use TV As An Audio Converter
    Most View
    -  Windows Small Business Server 2011 : Managing User Roles
    -  Canon IXUS 510 HS
    -  Review: Nikon D4 – The master of the dark arts (Part 2)
    -  Sharepoint 2007: Use the My Links to Manage Your Links
    -  Every Cloud...(Part 2) - Mobility & Portability
    -  Searching for Google’s future (Part 1) - Taking the tablets
    -  Dell Inspiron 17R Special Edition - Battery Is A Disappointment
    -  10 Valuable TVs For Your Living Room
    -  Silverlight Recipes : Creating Silverlight Using Ruby, Python, or JScript
    -  Build A Home Theatre PC (Part 2)
    -  The Library In Your Pocket (Part 2) - iOS – iPad, Android – Kobo Vox, Kindle Fire, Binatone ReadMe, Google Nexus 7
    -  Samsung 830
    -  Windows 9 : What to expect - 32-bit support , WinRT & XNA
    -  Programming with DirectX : View Transformations
    -  Securing Your Silverlight Application
    -  Programming the Mobile Web : Mobilizing WordPress and Other CMSs
    -  Aerocool Silent Master Blue
    -  Getting Started with Device Manager
    -  Calendar Plus - Simple And Friendly
    -  Transact-SQL in SQL Server 2008 : New date and time Data Types and Functions