temporary file in C#
-
Hi All, I was just looking through MSDN and got a shock in that there is no such thing as a temporary file .. (only a memorystream) I need someting like the C++ tmpfile() ... I need to save some to a temp file as I have a third-party plugin that can only read an XML file if I specify a filename, therefore I need a temp file on the disk ... Is there still a way to do this ? Thanks kort
-
Hi All, I was just looking through MSDN and got a shock in that there is no such thing as a temporary file .. (only a memorystream) I need someting like the C++ tmpfile() ... I need to save some to a temp file as I have a third-party plugin that can only read an XML file if I specify a filename, therefore I need a temp file on the disk ... Is there still a way to do this ? Thanks kort
Check this out http://www.developerfusion.co.uk/show/3913/[^] Would this address your requirement?
Vasudevan Deepak Kumar Personal Homepage Tech Gossips
-
Hi All, I was just looking through MSDN and got a shock in that there is no such thing as a temporary file .. (only a memorystream) I need someting like the C++ tmpfile() ... I need to save some to a temp file as I have a third-party plugin that can only read an XML file if I specify a filename, therefore I need a temp file on the disk ... Is there still a way to do this ? Thanks kort
I find that the following code can be helpful. This class creates a temporary file that you can use, which is removed once you dispose the instantiating class.
; /// <summary> /// Create a temporary file. The file will be removed as soon as /// the instance of this class is disposed. /// </summary> public class TempFile : IDisposable { private string _fileName; private bool disposed = false; /// <summary> /// Instantiate a new instance of <see cref="TempFile"/>. /// </summary> public TempFile() { _fileName = Path.GetTempFileName(); } /// <summary> /// Get the temporary file name. /// </summary> public string FileName { get { return _fileName; } } #region IDisposable Members /// <summary> /// Dispose of the temporary file. /// </summary> public void Dispose() { Dispose(true); } protected void Dispose(bool dispose) { if (dispose && !disposed) { if (File.Exists(_fileName)) { try { File.Delete(_fileName); } catch (IOException ex) { Trace.WriteLine(string.Format("Unable to remove temporary file because of {0}", ex.Message)); } } disposed = true; GC.SuppressFinalize(this); } } #endregion } using (TempFile tempFile = new TempFile()) { filename = tempFile.FileName; using (FileStream fs = new FileStream(tempFile.FileName, FileMode.Create)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.WriteLine("Write to temp."); } fs.Close(); } Console.WriteLine("The size of {0} is {1} bytes", tempFile.FileName, new FileInfo(tempFile.FileName).Length); }
Deja View - the feeling that you've seen this post before.
-
Hi All, I was just looking through MSDN and got a shock in that there is no such thing as a temporary file .. (only a memorystream) I need someting like the C++ tmpfile() ... I need to save some to a temp file as I have a third-party plugin that can only read an XML file if I specify a filename, therefore I need a temp file on the disk ... Is there still a way to do this ? Thanks kort
To add to the 2 other answers, in .NET 2.0, there are new constructors added to
System.IO.FileStream
. 2 of them take aFileOption
argument (can be bitwised). One of the members of this enumeration isDeleteOnClose
. I leave as an excercise for you to figure out what this does ;)----- You seem eager to impose your preference of preventing others from imposing their preferences on others. -- Red Stateler, Master of Circular Reasoning and other fallacies If atheism is a religion, then not collecting stamps is a hobby. -- Unknown God is the only being who, to rule, does not need to exist. -- Charles Baudelaire