Using TempFileCollection

  • Thread starter Thread starter Claire
  • Start date Start date
C

Claire

How do I use the TempFileCollection class please?
The m/s halp files say i can use it to generate temp files but looking
through the help files, i can't actually see an obvious method that does
this.
I just need to create a temp file for writing data from a memory stream to a
file stream.
A code sample would be appreciated

thanks
 
Hi,

I had never heard of that class, when I need a temp file I use
Path.GetTempFileName , you also have Path.GetTempPath


Cheers,
 
Its some class in System.CodeDom.Compiler... not sure what it does but
surelly it is not meant to be used for this purpose :)
 
thanks for answering
It looked useful as it's supposed to delete the temporary files after use.
Ive used path.GetTempFilename, but it would be useful to know how and why to
use it
 
Hi Claire,

I also did not have much experience on this TempFileCollection class.
However, based on my research, TempFileCollection is normally used with
Compilation process. It can manage a collection of files with the same name
and different extensions, we can use TempFileCollection.AddExtension method
to specify the extensions we want to manage in the collection. We can also
use TempFileCollection.AddFile method to add other files in the collection.
Additionally, we can use TempFileCollection.KeepFiles property to specify
if the files in the collection should be deleted.

I can find 2 articles make use of this class, for your information:
"Compiling code during runtime"
http://www.codeproject.com/csharp/CodeCompilation.asp
"VB060004 - Understanding and Using System.Reflection.Emit"
http://www.codenotes.com/articles/articleAction.aspx?articleID=506

For Path.GetTempFileName method, if we use Reflector to view its source
code, we will see that:
//Path.GetTempFileName
public static string GetTempFileName()
{
string text1 = Path.GetTempPath();
StringBuilder builder1 = new StringBuilder(260);
uint num1 = Win32Native.GetTempFileName(text1, "tmp", 0, builder1);
if (num1 == 0)
{
__Error.WinIOError();
}
return builder1.ToString();
}
//Path.GetTempPath
public static string GetTempPath()
{
new EnvironmentPermission(PermissionState.Unrestricted).Demand();
StringBuilder builder1 = new StringBuilder(260);
uint num1 = Win32Native.GetTempPath(260, builder1);
string text1 = builder1.ToString();
if (num1 == 0)
{
__Error.WinIOError();
}
return text1;
}
Path.GetTempPath internally encapsulate WIn32 GetTempPath API. From the
Win32 GetTempPath API, we can see that:

The GetTempPath function checks for the existence of environment variables
in the following order and uses the first path found:
The path specified by the TMP environment variable.
The path specified by the TEMP environment variable.
The path specified by the USERPROFILE environment variable.
The Windows directory.

So, Path.GetTempFileName actually lets the system create a temp file in
system temp directory with filename marked with "tmp". However, when the
system shuts down, temporary files whose names have been created by this
function are not automatically deleted.

I am not sure what function you really want. If you wanted to get the temp
file automatically deleted after it is closed, I think we may P/invoke
CreateFile Win32 API with FILE_FLAG_DELETE_ON_CLOSE flag specified. This
flag will force the system to delete the file immediately after all of its
handles are closed

Hope this helps
============================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top