Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. temporary file in C#

temporary file in C#

Scheduled Pinned Locked Moved C#
csharpc++xmlquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kaminem
    wrote on last edited by
    #1

    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

    V P L 3 Replies Last reply
    0
    • K kaminem

      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

      V Offline
      V Offline
      Vasudevan Deepak Kumar
      wrote on last edited by
      #2

      Check this out http://www.developerfusion.co.uk/show/3913/[^] Would this address your requirement?

      Vasudevan Deepak Kumar Personal Homepage Tech Gossips

      1 Reply Last reply
      0
      • K kaminem

        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

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • K kaminem

          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

          L Offline
          L Offline
          Le centriste
          wrote on last edited by
          #4

          To add to the 2 other answers, in .NET 2.0, there are new constructors added to System.IO.FileStream. 2 of them take a FileOption argument (can be bitwised). One of the members of this enumeration is DeleteOnClose. 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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups