Copy temp file before it's deleted
I had an idea to write some useful code so that the recently downloaded temp files like installation files and other media files can be copied to somewhere safe location before it gets deleted.
string dir = "c:\\Users\\neal\\appdata\\Local\\Temp";
string newdir = "D:\\"开发者_运维知识库;
var directory = new DirectoryInfo(dir);
var myFile = (from f in directory.GetFiles()
orderby f.LastAccessTime descending
select f).First();
var myDir = (from f in directory.GetDirectories()
orderby f.LastAccessTime descending
select f).First();
myFile.CopyTo(newdir, true);
The above method doesn't actually work. And I'm not sure why. I guess every file may not have access rights (installation files).
Any idea or any other logic out there?
I definitely agree that there are already proven solutions to this, however if you wish to implement something in code you should check out the FileSystemWatcher http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
This will notify your program of changes to the directory you are watching so you can take action.
精彩评论