Refer to the MSDN documentation for a full list of methods and properties.

To see how to use the DirectoryInfo class, consider the following example:

static void Main(string[] args) {

 string path = @'C:My Folder';

 DirectoryInfo di = new DirectoryInfo(path);

 try {

  //---if directory does not exists---

  if (!di.Exists) {

   //---create the directory---

   di.Create(); //---c:My Folder---

   //---creates subdirectories---

   di.CreateSubdirectory('Subdir1'); //---c:My FolderSubdir1---

   di.CreateSubdirectory('Subdir2'); //---c:My FolderSubdir2---

  }

  //---print out some info about the directory---

  Console.WriteLine(di.FullName);

  Console.WriteLine(di.CreationTime);

  //---get and print all the subdirectories---

  DirectoryInfo[] subDirs = di.GetDirectories();

  foreach (DirectoryInfo subDir in subDirs)

   Console.WriteLine(subDir.FullName);

  //---get the parent of C:My folder---

  DirectoryInfo parent = di.Parent;

  if (parent.Exists) {

   //---prints out C:---

   Console.WriteLine(parent.FullName);

  }

  //---creates C:My FolderSubdir3---

  DirectoryInfo newlyCreatedFolder =

   di.CreateSubdirectory('Subdir3');

  //---deletes C:My FolderSubdir3---

  newlyCreatedFolder.Delete();

 } catch (IOException ex) {

  Console.WriteLine(ex.Message);

 } catch (Exception ex) {

  Console.WriteLine(ex.Message);

 }

 Console.ReadLine();

}

In this example, you first create an instance of the DirectoryInfo class by instantiating it with a path (C:My Folder). You check if the path exists by using the Exist property. If it does not exist, you create the folder (C:My Folder) and then create two subdirectories underneath it (Subdir1 and Subdir2).

Next, you print out the full pathname (using the FullName property) of the folder and its creation date (using the CreationTime property). You then get all the subdirectories under C:My Folder and display their full pathnames. You can get the parent of the C:My Folder using the Parent property.

Finally, you create a subdirectory named Subdir3 under C:My Folder and pass a reference to the newly created subdirectory to the newlyCreatedFolder object. You then delete the folder, using the Delete() method.

Directory Class

The Directory class is similar to DirectoryInfo class. The key difference between is that Directory exposes static members instead of instance members. The Directory class also exposes only methods — no properties. Some of the commonly used methods are described in the following table.

Method Description
CreateDirectory Creates a subdirectory.
Delete Deletes a specified directory.
Exists Indicates if a specified path exists.
GetCurrentDirectory Gets the current working directory.
GetDirectories Gets the subdirectories of the specified path.
GetFiles Gets the file list from a specified directory.
SetCurrentDirectory Sets the current working directory.

Refer to the MSDN documentation for a full list of methods and properties.

Here's the previous program using the DirectoryInfo class rewritten to use the Directory class:

static void Main(string[] args) {

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату