Create a temporary file

Posted by Bubbly under C# category on | Views : 2925
This subrouting Creates a Temporary file and sets its Attribute property to Temporary.
    private void btnCreateTempFile_Click(object sender, System.EventArgs e)
// Clear the Status Bar
this.sbrStatus.Text = string.Empty;
// return the path and name of a newly created Temporary file. Note that the GetTempFileName() method actually creates a 0-byte file and returns the name of the created file.
_flnm = Path.GetTempFileName();

// Craete a FileInfo object to manipulate properties of the created temporary file
FileInfo myFileInfo = new FileInfo(_flnm);

// Use the FileInfo object to set the Attribute property of this
// file to Temporary. Although this is not completely necessary,
// the .NET Framework is able to optimize the use of Temporary
// files by keeping them cached in memory.
// Inexplicably, the Attribute given to a file created with
// the GetTempFileName() method is Archive, which prevents the
// .NET Framework from optimizing its use.
myFileInfo.Attributes = FileAttributes.Temporary;
catch(Exception exc)
// Warn the user if there is a problem
this.sbrStatus.Text = exc.Message;

// Display the Temporary Filename in the txtTempFile text box.
this.txtTempFile.Text = _flnm;

Comments or Responses

Login to post response