My question is about handling temporary files in a small .NET program. This program/utility just does the following with two URLs:
我的问题是在一个小的.NET程序中处理临时文件。此程序/实用程序只使用两个URL执行以下操作:
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.
这很好用 - 因为WinMerge只需要在只读模式下运行,所以可以通过多次运行我的程序来覆盖文件,并且没有任何不好的事情发生。
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?
但是,我现在想将临时文件名更改为有意义的(与URL相关),以便我可以看到WinMerge视图中的哪个。我还想在不再需要时清理这些文件。我有什么选择?
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.
谢谢。
1 个解决方案
#1
5
Create a Guid
-based folder under the user's temp area and use that?
在用户的临时区域下创建一个基于Guid的文件夹并使用它?
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);}
}
#1
5
Create a Guid
-based folder under the user's temp area and use that?
在用户的临时区域下创建一个基于Guid的文件夹并使用它?
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);}
}