Wednesday, April 11, 2012

Checking and creating a directory

Almost all applications need to write "stuff" to a certain directory. The directory can store images, configuration files, databases,... whatever,...
So here in this example we'll create a "Temp" directory in the application path.

public String determineTempDirectory()
{
     String TempDirectory;


     TempDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
     TempDirectory+="\\Temp";


     if(!System.IO.Directory.Exists(TempDirectory))
          {
               System.IO.Directory.CreateDirectory(TempDirectory);
          }


     return TempDirectory;
}


So, by dissecting this code, we can see that what this function does is to return the path of a "Temp" directory, located on the same location as the application. The function checks if the "Temp" directory already exists and if doesn't then, creates it.

No comments:

Post a Comment