Using the temp file in Managed C + +

Source: Internet
Author: User

in Managed C + + Use temp in file


         The program sometimes avoids the need to use temporary files, but at Microsoft. NET platform, many programmers do not use the convenience of the path object to process temporary files, still manually determine the unique file name in the current directory of the application, and then delete the program after it has been run out.
         The trick that this article demonstrates is that by using the path class, with one or two lines of code, you can accomplish the following tasks:

1,Locate the Temp directory.
2,Create a unique, optimized temporary file.
3,Delete temporary files when you are finished using them.

Locate the Temp the directory
         To determine the "temp" directory, you can use the Path::gettemppath static method, where it is important to note that calls to this method are placed in a try/block block, because based on the permissions of the current user, It is likely that a SecurityException (security exception) will be thrown.

using namespace system::security;
using namespace System::IO;

...

String TempFolder;
Try
{
TempFolder = Path::gettemppath ();
}
catch (securityexception* ex)
{
It probably means you don't have the rights you need.
}
catch (exception* ex)
{
Handle all other exceptions
}


         Create and optimize temporary files
         Here you can use Path::gettempfilename to get a unique name for the temporary file, which creates a file and returns the name of the most recently created file.
         File attributes are set to "archive", essentially to prevent. NET, so if you change the file attribute to something else, you can get it from the. NET runtime (runtime) cache for a small point of performance improvement.
         To begin, use a temporary filename to construct a FileInfo object and set the FileInfo attributes to Fileattributes::temporary:

using namespace system::security;
using namespace System::IO;

...

string* FileName;
filename->empty;

Try
{
Create a temporary file of zero length
FileName = Path::gettempfilename ();

Set the file property to "temporary" for better performance
fileinfo* myfileinfo = new FileInfo (fileName);
Myfileinfo->attributes = fileattributes::temporary;

...
}
catch (exception* ex)
{
Handling Exceptions
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.