问题描述
我想删除当前用户的临时文件夹中的所有临时文件。为此我编写了以下代码
string s = System.IO.Path.GetTempPath() ;
Console.WriteLine(s);
DirectoryInfo di = new DirectoryInfo(s);
FileInfo [] fi = di.GetFiles();
foreach (FileInfo f in fi)
{
f。删除();
}
然后我遇到了一些问题,某些进程可能会使用某些文件,最后它们不会被删除,例外情况会是触发线
f.delete()
我以为我是'' d最好获取捕获每个文件并杀死它的进程ID。问题是如何获得该流程ID。
OP的附加信息来自非解决方案
当我知道捕获文件的进程时,我可以激活代表此进程的屏幕并要求用户保存/取消,或者如果后台进程我可以警告用户此进程捕获此文件和你可能会失去工作。具有此功能的程序类似于Windows执行的SHUT DOWN警告过程。
I wanted to delete all temporary files in the temp folder of the current user. For this purpose I wrote the following code
string s = System.IO.Path.GetTempPath(); Console.WriteLine(s); DirectoryInfo di = new DirectoryInfo(s); FileInfo[] fi = di.GetFiles(); foreach (FileInfo f in fi) { f.Delete(); }
I then was faced with a problem that some files may be used by some processes and finally they won''t be deleted and an exception would be trigger at the line
f.delete()
I thought that I''d better get the process ID that catches each file and kill it. The question is how to get that process id.
OP''s additional information moved from non-solution below
When I know the process that catches a file I can activate the screen that represent this process and ask the user to save/cancel , or if the process in the background I can warn the user that the this process catches this file and you may lost work. This program with this ability would be similar to the SHUT DOWN warning process that the windows do.
这篇关于如何获取捕获特定文件的进程ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!