Working with the Directorylnfo class, Creating objects...

Working with the Directorylnfo class

The DirectoryInfo class contains a set of methods for creating, moving, deleting, and executing enumeration of all folders and subfolders. In this class, the following basic features are added to the functionality of the Base class FileSystemlnfo (Table 9.2).

Table 9.2

Basic elements of the Directorylnfo class

Elements

Description

Create (),

CreateSubdi -

rectory ()

create a folder (or set of subfolders) using a given name

Delete ()

deleting a folder and all its contents

GetDirec-tories ()

getting an array of strings that contain the names of all subfolders of the current folder

GetFiles ()

receiving an array of objects of type Filelnfo, which correspond to files of a specific folder

MoveTo ()

moving the folder and its contents to a new location

Parent

getting the Directorylnfo object of the parent folder for the current folder

Root

Getting the Directorylnfo object for the root folder of the current folder

Creating DirectoryInfo objects

Working with the Directorylnfo class begins with the creation of an object of this class using the constructor, the parameter of which is the path to the specific folder. To get the object corresponding to the current folder (for example, the directory of the executable application), use the string . "For example:

// get the current working directory Directorylnfo dir1 = new Directorylnfof. );

// get the object corresponding to the C: Windows Directory directory dir2 = new Directorylnfo (@ C: Windows );

If the specified folder does not exist, the object will be created, but when working with it, the System exception will be thrown. IO.DirectoryNotFoundException.

If an object is created for a non-existent folder, you need to call the Create () method before continuing to work with the object:

// Create an object for a directory that does not exist Directorylnfo dir3 = new Directorylnfo (@ C: MyFolder );

// Then create this folder dir3.Create ();

After creating an object of class Directorylnfo, you can work with its contents using any properties inherited from the FileSystemlnfo class. For example, the DirectoryInfo object associated with the C: Windows folder provides a lot of useful information about it: Directorylnfo dir = new Directorylnfo (@ C: WindowsH); Console.WriteLinefFullName: {0} , dir.FullName);

Console.WriteLinefName: {0} & quot ;, dir.Name);

Console.WriteLine ( Parent: {0} & quot ;, dir.Parent);

Console.WriteLine ( Attributes: {0} & quot ;, dir.Attributes);

thematic pictures

Also We Can Offer!

Other services that we offer

If you don’t see the necessary subject, paper type, or topic in our list of available services and examples, don’t worry! We have a number of other academic disciplines to suit the needs of anyone who visits this website looking for help.

How to ...

We made your life easier with putting together a big number of articles and guidelines on how to plan and write different types of assignments (Essay, Research Paper, Dissertation etc)