Tuesday, September 3, 2013

C# - Working with File System Objects(Part 3)

This article is the third from the “Working with File System Objects” series (the first two articles covered the directory classes), this article will cover the “File” class that located under the system.Io namespace.

The ‘File’ Class
This class exposing static methods to handle files operations.

Note!
To use this class please add the “System.IO” namespace.

Class Summary:
This table contain the main methods you can use while working with this class.

Method Name
Why to use?
File. Create
When you need to Create a new file
File. SetAccessControl
When you need to apply ACL changes
File. Copy
When you need to copy file to different location
File. Delete
When you need to delete a file
File. Exists
When you to need validate if file located in File system
File. Move
When you need to Move file to different location
File. GetCreationTime
When you need the file creation time
File. GetLastAccessTime
When you need the file last accessed  time
File. GetLastWriteTime
When you need the last modification made on file
File. Replace
When you need to replace content from file ‘a’ to ‘b’
File .AppendAllLines
When you need to Add multiple strings to a file
File .AppendAllText
When you need to Add single string to a file
File .ReadAllLines
When you need to read all lines from a file(1)
File .ReadAllText
When you need to read all lines from a file(2)

Code Examples:

Create a new Txt file

This code example will demonstrate how to add a new file on a specific path

static  void Main(string[] args)
        {
            File. Create("c:\\NewFile.txt");//Creating a new file
        }


Create a new Txt file with specific file options

This code demonstrate how to create a new file with specific file options, the file option is a simple ‘Enum’ that can be used to define different file options while creating the file.
This code example will demonstrate how to add a new file with the availability to read/write A synchronically

static  void Main(string[] args)
        {
            File.Create("c:\\1.txt",1024,FileOptions.Asynchronous);
        }

Create a new Txt file with security options

Continuing the previous paragraph, you also can add specific security values to a file.

Note!
To add security options you also need the buffer size and the file option to overloading the ‘File’ class constructor.

In this example we create a new file with specific permissions for the “Everyone” group.

Step 1 - Creating the security object:
FileSecurity FileSecuority = new FileSecurity();
FileSecuority.AddAccessRule(new FileSystemAccessRule(@"Everyone", FileSystemRights.FullControl, AccessControlType.Allow));

Step 2 - Creating the file with the security object:

File.Create("c:\\FileName.txt", 333, FileOptions.Asynchronous, FileSecuority);

Result:
























Manipulate file permissions
This section will cover multiple issues related to file permissions, from what I saw over the internet this part is the clearest from all other.

Note!
Please add the following namespace “System.Security.AccessControl;

Example 1:
This example will display how to add a new ACL to a folder, in our example we add the administrator user with deny permissions on ‘Read’.

Code review:
Line 1: creating the file security object.

Line 2: Initialize the Security object with the relevant attributes:
User that we want to manipulate
Configuring the relevant system permission
Set the access permission (Allow\Deny)

Line 3: Set the new ACL on file

Note!
You must specify a valid user name otherwise the operation will failed.

class Program
    {
        static void Main(string[] args)
        {
            FileSecurity AddAcl = new FileSecurity();
     AddAcl.AddAccessRule(new FileSystemAccessRule(@"Administrator",   FileSystemRights.Read, AccessControlType.Deny));
            File.SetAccessControl("c:\\1.txt", AddAcl);
        }

Result:
























Example 2:
This code will set the ‘Full Control’ permission to the administrator

static void Main(string[] args)
        {
            FileSecurity AddAcl = new FileSecurity();
            AddAcl.AddAccessRule(new FileSystemAccessRule(@"Administrator", FileSystemRights.FullControl, AccessControlType.Allow));
            File.SetAccessControl("c:\\1.txt", AddAcl);
        }

Result:
























Example 3:
This example will show how to remove an ACL from a folder

static void Main(string[] args)
        {
            FileSecurity AddAcl = new FileSecurity();
            AddAcl.RemoveAccessRule(new FileSystemAccessRule(@"Administrator", FileSystemRights.FullControl, AccessControlType.Allow));
            File.SetAccessControl("c:\\1.txt", AddAcl);
        }

Result:















Example 4:
This code example will remove all permissions for specific user.

Before: 
























After running code:

static void Main(string[] args)
        {
            FileSecurity AddAcl = new FileSecurity();
            AddAcl.RemoveAccessRuleAll(new FileSystemAccessRule(@"Administrator", FileSystemRights.Read, AccessControlType.Allow));
            File.SetAccessControl("c:\\1.txt", AddAcl);
        }





No comments:

Post a Comment

My Presentations