Using the Path object in managed C + + to process temp files

Source: Internet
Author: User

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 files.

3, after the use of the deletion of temporary files.

Locate the Temp 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)
{
  //很可能意味着你没有所需的权限
}
catch(Exception* ex)
{
  //处理其他所有异常
}

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
{
  //创建一个长度为零的临时文件
  fileName = Path::GetTempFileName();
  //把文件属性设为"Temporary"以获得更好的性能
  FileInfo* myFileInfo = new FileInfo(fileName);
  myFileInfo->Attributes = FileAttributes::Temporary;
  ...
}
catch(Exception* ex)
{
  //处理异常
}

Use and delete temporary files

Once you create a temporary file, you can use it just as you would with other files, for example, by inserting the following code in the Try/block block above, which uses the StreamWriter class to write a simple string in a temporary file:

//向临时文件中写入数据
StreamWriter* writer = File::AppendText(fileName);
writer->WriteLine("A test sample data");
writer->Flush();
writer->Close();

The data written can be read through the following StreamReader, where the contents of the entire file are read into a string object:

StreamReader* reader = File::OpenText(fileName);

Finally, after using the temporary file, you can use file::D elete to delete, simply pass in the file name on the line:

File::Delete(fileName);

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.