views:

118

answers:

1

My question is about handling temporary files in a small .NET program. This program/utility just does the following with two URLs:

Download each URL to a string (WebClient.DownloadString).
Save each string to a temporary file.
Call WinMerge (Process.Start) to diff the two files (in read-only mode for both files).

I currently have the Simplest Thing That Could Possibly Work:

save URL1 to windows/temp/leftFileToDiff.txt
save URL2 to windows/temp/rightFileToDiff.txt.

This works great - as WinMerge only needs to run in Read Only mode the files can be overwritten by running my program multiple times and nothing bad happens.

However, I would now like to change the temporary file names to something meaningful (related to the URL) so that I can see which is which in the WinMerge view. I also want to clean these files up when they are no longer needed. What are my options for this?

My next simplest idea is to have a specified folder where these are stored and just to zap this every time my program exits. Is there a better/more elegant/standard way?

Thanks.

+3  A: 

Create a Guid-based folder under the user's temp area and use that?

        string path = Path.Combine(Path.GetTempPath(),
              Guid.NewGuid().ToString("n"));
        Directory.CreateDirectory(path);
        try
        {
            // work inside path
        }
        finally
        {
            try { Directory.Delete(path, true); }
            catch (Exception ex) {Trace.WriteLine(ex);}
        }
Marc Gravell
Instead of the GUID i would create the path with string path = Path.Combine(Path.GetTempPath(), System.IO.Path.GetRandomFileName());
Oliver
That works too ;-p
Marc Gravell
Thanks - I'll try this out
Rock and or Roll