Thursday, September 5, 2013

C# - Path class (Part 1)

This article will be dedicated to the ‘Path’ class located under the “System.Io” namespace.

As explained in the first articles for this series, the “System.io” namespace provide really great ways to control file system objects (Folders/Files). The path class contain additional ways to control/manipulate the objects paths using a dedicated strings.

The main operations you can accomplish while using this class will be explained in the following paragraphs.

Note!
The ‘path’ class is configured by design as ‘Static’, therefore you can use it without creating a ‘path’ class instance.

Changing the file extension
Using this method is a little tricky, the method is not actually change the file extension on your file system, the method actually perform the change in the application memory that will be used when creating the new file(the original file remain on your disk  and new file should be created in addition).

This method can be used when you need to change extension for a specific file name and in addition it’s could save some other code lines because the file location saved in memory (send reference to the file properties) therefore you can manipulate it without the need to add multiple code lines (please see my examples).

Method parameters
The method receive only 2 parameters:

String Path – location for the source file.

Extension – the new extension that deployed on the new file.

The extensions you can use:
.ExtensionName – valid extension like ‘doc’,Xls’ ,pdf’ …

Null – when using null the new file will be created without extension.

Example 1:
This example will demonstrate how to use the method with the creation of both file types (With valid extension or file without extension).

Folder status before manipulations:




Running the code:
class Program
    {
        static void Main(string[] args)
        {
            //Step 1: determine the Source File

            string FileLocation = "C:\\ChangeExtension\\ExampleFile.txt";

            //Step 2: Changing the path in memory and adding a new files
    
            //File Without Extension:

            File.WriteAllText(Path.ChangeExtension(FileLocation, null),"File 1");
      
            //File with specific Extension:

            File.WriteAllText(Path.ChangeExtension(FileLocation, ".doc"), "File 2");
        }
    }

Folder status after manipulations:

 
Example 2:
This example will demonstrate how to use single file to create a large file tree without the need to replicate your code per extension.
Folder status before manipulations:

Running the code:
        static void Main(string[] args)
        {
            //Creating an array of file extensions:
            string[] FileExtensions = new string[] {".doc",".docx","pdf",".jpg","log"};
            //Step 1: determine the Source File
            string FileLocation = "C:\\ChangeExtension\\ExampleFile.txt";
            //Step 2: Changing the path in memory and adding a new files       
            foreach (string extension in FileExtensions)
            {
                File.WriteAllText(Path.ChangeExtension(FileLocation, extension), "");   
            }
        }
    }
}
Folder status after manipulations:




No comments:

Post a Comment

My Presentations