Find path of TEMP directory

Posted by Bubbly under C# category on | Views : 4141
This subroutine finds the path to the Temp directory on the local machine. It is ALWAYS important to wrap any Path
method calls in a try-catch block since exceptions are thrown based on the permissions granted to the current user.
private void btnFindTempDirectory_Click(object sender, System.EventArgs e)
// initialize to an empty string.
string tempPthstr = string.Empty;

// Clear the Status Bar.
this.sbrStatus.Text = string.Empty;

// Attempt to get the path to the Temp directory
tempPthstr = Path.GetTempPath();
catch(SecurityException sex)
// Show error to user, if they don't have the proper permissions
this.sbrStatus.Text = "Required Message here";
catch(Exception exc)
// Show error to user.
this.sbrStatus.Text = "Required Message her";

// Set txtTempDirectory equal to the Temp path.
this.txtTempDirectory.Text = tempPthstr;

Comments or Responses

Login to post response