Saqib Ullah

BootStrapper Know How

  Home  |   Contact  |   Syndication    |   Login
  93 Posts | 1 Stories | 341 Comments | 15 Trackbacks

News



Article Categories

Archives

Post Categories

Blogging websites

Favourite Blogs

Private Links

Sites

This following code create a file and give right only to the "localAdmin" user in my case “localAdmin” is administrator. Next time if you login from guest account and try to access the same file so you will get error message of access deined because user not have the rights.

using System;
using System.IO;
using System.Data;
using System.ComponentModel;
using System.Security.AccessControl;
using System.Security.Principal;
 
namespace Permission
{
    public partial class CreatePermissoinFile
    {
        ///<summary>
        /// Create File function
        ///</summary>
        ///<param name="fileName">Name of file</param>
        public void CreateFile(string fileName)
        {
            //localAdmin user has Administrator priviliges
            NTAccount user = new NTAccount("localAdmin");
            FileSystemAccessRule ar = new FileSystemAccessRule(user, FileSystemRights.FullControl, AccessControlType.Allow);
            FileSecurity fs = new FileSecurity();
            fs.AddAccessRule(ar);
            //Name of the File with extension
            //like: test.doc
            System.IO.File.Create(fileName, 1000, FileOptions.None, fs);
        }
    }
}

Working
Call CreatePermissionFile class and login from non-administrative account into your computer. Open created file and you will get “Access is denied” error.
 
Reason
Because login user is non-administrative and doesn’t have right to open and move.
posted on Monday, May 28, 2007 7:47 PM