[英]C# Cannot Delete File - System.UnauthorizedAccessException
I am running a separate process that minimizes a folder of javascript files. 我正在运行一个单独的过程,以最小化javascript文件的文件夹。 This process creates a new file for each of the js files with "_min.js" appended to it.
此过程将为每个js文件创建一个新文件,并附加“ _min.js”。 The next step is to delete the old js files (the ones without "_min.js").
下一步是删除旧的js文件(不带“ _min.js”的文件)。 For some reason File.Delete cannot delete these files.
由于某种原因,File.Delete无法删除这些文件。
It would appear that some process still has a handle on these files. 看来某些进程仍然可以处理这些文件。 I get a System.UnauthorizedAccessException exception when I attempt the delete.
尝试删除时,出现System.UnauthorizedAccessException异常。 I have sufficient privileges to this folder.
我对此文件夹有足够的特权。 Can someone tell me what I am overlooking?
有人可以告诉我我忽略了什么吗?
I am running the process several times in this loop. 我在此循环中多次运行该过程。
foreach (var fileInfo in jsFiles)
{
var outFileName = fileInfo.FullName.Replace(".js", "_min.js");
var compressorPath = "\"C:\\Dev\\Team Interactive Tools\\trunk\\Infrastructure\\MsBuild\\lib\\yuicompressor-2.4.2.jar\"";
StringBuilder stringBuilder = new StringBuilder("-jar " + compressorPath + " ");
stringBuilder.Append("\"" + fileInfo.FullName + "\"");
stringBuilder.Append(" -o " + "\"" + outFileName + "\"");
Process p = new Process();
p.StartInfo.FileName = "\"C:\\Program Files (x86)\\Java\\jre6\\bin\\java\"";
p.StartInfo.Arguments = stringBuilder.ToString();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
}
return true;
}
Then I try to remove the orginals: 然后,我尝试删除原始文件:
private void RemoveOrginalJs(FileInfo[] files)
{
foreach (var fileInfo in files)
{
File.Delete(fileInfo.FullName);
}
}
I am tried Process.close() after each process run but it makes no differance. 在每个进程运行之后,我都尝试过Process.close(),但没有区别。
I don't know the tool that you are starting, but waiting for its completion somehow seems the right thing to do: 我不知道您要启动哪个工具,但是等待它完成似乎是正确的选择:
p.Start();
p.WaitForExit();
Maybe the threads still have the file locked? 也许线程仍然将文件锁定了? Do you wait for them to finish before you try and delete?
在您尝试删除之前,您是否等待它们完成?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.