October 1, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Managed C++: Working with Temp Files

  • February 18, 2005
  • By Tom Archer
  • Send Email »
  • More Articles »
As the owner of a software-development consulting company, I'm afforded the opportunity to work on many systems, both in terms of architecting and designing as well as coding. One thing that I see quite frequently is that many coders who are relatively new to .NET are still not taking advantage of the Path object when dealing with temporary files. Specifically, they still manually determine a unique name for the file within the application's current folder and then delete it when done. This tip illustrates the following tasks that you can achieve with as little as one or two lines of code using the Path class:
  • Locate the "temp" folder
  • Create a unique and optimized temporary file
  • Use and delete a temporary file

Locate the "Temp" Folder

In order to determine the "temp" folder, you can use the static Path::GetTempPath method. The main thing to note here is that you need to place any call to this method in a try/catch block, as a SecurityException can be thrown based on the current user's permissions:
using namespace System::Security;
using namespace System::IO;

...

String tempFolder;
try 
{
  tempFolder = Path::GetTempPath();
}
catch(SecurityException* ex)
{
  // probably means that you don't have the required permissions
}
catch(Exception* ex)
{
  // handle all other exceptions
}

Create a Unique and Optimized Temporary File

You can use the Path::GetTempFileName to retrieve the name of a unique temporary file. While not obvious from the name, this method will also create the file and return the name of the newly created file.

The file attribute is set to "Archive", which actually prevents .NET from optimizing its usage. Therefore, you can gain a small performance benefit by modifying the file's attribute so that the .NET runtime caches the file.

To do that, first construct a FileInfo object using the name of the temporary file and then set the FileInfo object's Attributes property to FileAttributes::Temporary:

using namespace System::Security;
using namespace System::IO;

...

String* fileName;
fileName->Empty;

try 
{
  // Create a 0-length temporary file
  fileName = Path::GetTempFileName();

  // Set the file's attributes to "Temporary" for
  // better performance
  FileInfo* myFileInfo = new FileInfo(fileName);
  myFileInfo->Attributes = FileAttributes::Temporary;

  ...
} 
catch(Exception* ex)
{
  // handle exceptions
}

Use and Delete a Temporary File

Once you've created the temporary file, use it as any other file. For example, using the code from the previous section, you could insert the following code at the bottom of the try block in order to write a simple string to the temporary file using the StreamWriter class:
...

// Write data to the temp file
StreamWriter* writer = File::AppendText(fileName);
writer->WriteLine("Sample data");
writer->Flush();
writer->Close();
This data can then be retrieved via a StreamReader as follows, where you're reading the contents of the entire file into a String object:
StreamReader* reader = File::OpenText(fileName);
Finally, when you're finished using the temporary file, you can delete it via the File::Delete method, where you simply pass the name of the file:
File::Delete(fileName);

About the Author

Tom Archer owns his own training company, Archer Consulting Group, which specializes in educating and mentoring .NET programmers and providing project management consulting. If you would like to find out how the Archer Consulting Group can help you reduce development costs, get your software to market faster, and increase product revenue, contact Tom through his Web site.






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel