Managing Directories in .NET
page 1 of 3
Published: 20 May 2004
Unedited - Community Contributed
Using the System.IO namespace you can accomplish many actions such as deleting and copying files and directories. This article covers a few methods for managing file system directories in .NET.
by Steven Swafford
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 19116/ 62

Creating a Directory

System.IO provides all the necessary classes, methods, and properties for manipulating directories and files.  In order to utilize the various methods within System.IO, users attempting to perform these operations must also have the required permissions to accomplish these types of actions; if they do not, a SecurityException will be thrown.  Below you will find the main classes under this namespace.

Class Purpose/Use
1. BinaryReader and BinaryWriter - Read and write binary data.
2. Directory, File, DirectoryInfo, and FileInfo - Create, delete, and move files or directories. You can retrieve specific information about the files or folder by using of the properties defined in these classes.
3. FileStream -  Access the files in a random order.
4. MemoryStream - Access data stored in memory.
5. StreamWriter and StreamReader - Read and write textual information.
6. StringReader and StringWriter - Read and write textual Information from a string buffer.

If you have ever run across a requirement to create a directory on the server running your application, the System.IO namespace provides everything you need to accomplish such a task. Here I will show you two way to create a directory named MyDirectory on the C: Drive.

using System;
using System.IO;

namespace SystemIO
 /// <summary>
 /// Summary description for article051604.
 /// </summary>
 public class article051604

  static void Main(string[] args)

  public static void CreateDirectory1()

  public static void CreateDirectory2()
   string directoryPath = @"c:\MyDirectory";
   System.IO.DirectoryInfo di = Directory.CreateDirectory(directoryPath);


The first method, CreateDirectory1, simply utilizes the CreateDirectory method that takes a string parameter.  In my case I passed in @"C:\MyDirectory".

The second method, CreateDirectory2, performs exactly the same action as CreateDirectory1 with the only differences being I assigned @"C:\MyDirectory" to a string variable named directoryPath and then passed this variable into the CreateDirectory method.

Now that you are armed with the basics of creating a directory, we will now move into how to check if the directory in fact exist before we attempt to create it.

View Entire Article

User Comments

No comments posted yet.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-05-29 11:11:21 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search