Int*_*man 1 .net c# windows resources temporary-files
我的问题是在一个小的.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).
Run Code Online (Sandbox Code Playgroud)
我目前有可能工作的最简单的事情:
save URL1 to windows/temp/leftFileToDiff.txt
save URL2 to windows/temp/rightFileToDiff.txt.
Run Code Online (Sandbox Code Playgroud)
这很好 - 因为WinMerge只需要在只读模式下运行,所以可以通过多次运行我的程序来覆盖文件,并且没有任何不好的事情发生.
但是,我现在想将临时文件名更改为有意义的(与URL相关),以便我可以看到WinMerge视图中的哪个.我还想在不再需要这些文件时清理它们.我有什么选择?
我的下一个最简单的想法是拥有一个存储这些文件夹的指定文件夹,并且每次我的程序退出时都要删除它.有更好/更优雅/标准的方式吗?
谢谢.
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);}
}
Run Code Online (Sandbox Code Playgroud)