Free mag vol1 | Page 813

CHAPTER 20  FILE I/O AND OBJECT SERIALIZATION single function call. This method illustrates how to do so, extending the directory structure of the C: drive with some custom subdirectories: static void ModifyAppDirectory() { DirectoryInfo dir = new DirectoryInfo(@"C:\"); // Create \MyFolder off application directory. dir.CreateSubdirectory("MyFolder"); } // Create \MyFolder2\Data off application directory. dir.CreateSubdirectory(@"MyFolder2\Data"); If you call this method from within Main() and examine your Windows directory using Windows Explorer, you will see that the new subdirectories are present and accounted for (see Figure 20-2). Figure 20-2. Creating subdirectories You are not required to capture the return value of the CreateSubdirectory() method, but you should be aware that a DirectoryInfo object representing the newly created item is passed back on successful execution. Consider the following update to the previous method. Note the dot notation in the constructor of DirectoryInfo, which gives you access to the application’s installation point. static void ModifyAppDirectory() { DirectoryInfo dir = new DirectoryInfo("."); // Create \MyFolder off initial directory. dir.CreateSubdirectory("MyFolder"); // Capture returned DirectoryInfo object. DirectoryInfo myDataFolder = dir.CreateSubdirectory(@"MyFolder2\Data"); 759