c# 进程停止后无法删除日志文件
我的服务器上有许多进程,我试图停止、删除日志文件并最终重新启动。我的问题是,一旦所有进程停止(即使是写入日志文件的进程),我无法以编程方式删除日志文件(但我可以手动删除)。我收到的错误是“该进程无法访问该文件,因为它正在被另一个进程使用。即使我验证该进程已停止并将线程置于睡眠状态,我也无法删除该文件。
我尝试过 Controller.close() 控制器。重置();似乎没有任何作用。
public static void Stop()
{
foreach (var service in Services) {
Controller.ServiceName=service;
if (Controller.Status == ServiceControllerStatus.Stopped) {
Console.WriteLine("{0} was not running.", Controller.DisplayName);
}else {
Console.WriteLine("Stopping {0} on {1}", Controller.DisplayName, Controller.MachineName);
try {
Controller.Stop();
Controller.WaitForStatus(ServiceControllerStatus.Stopped);
} catch (InvalidOperationException) {
Console.WriteLine("Could not stop {0}", Controller.DisplayName);
}
Console.WriteLine("Service {0} now set to {1}", Controller.DisplayName, Controller.Status);
}
}
Console.WriteLine("\nDeleting Log Files on " + Controller.MachineName + " ...");
DeleteLogFiles();
}
private static void DeleteLogFiles()
{
foreach (var file in
Paths.SelectMany(path => FormatsToDelete, Directory.GetFiles).SelectMany(fileList => fileList)) {
try {
File.Delete(file);
}catch(IOException io) {
Console.WriteLine("\n" + io.Message);
}
}
}
}
}
I have many processes on a server which Im attempting to stop, delete the log files, and eventually restart. My problem is that once all the processes stop (even the one that writes to the log file), Im unable to programatically delete the log files (but I can manually). The error I receieve is "the process cannot access the file because it is being used by another process. Even when I verify the process has stopped and put the thread to sleep I cannot delete the file.
Ive tried Controller.close() controller.reset(); nothing seems to work.
public static void Stop()
{
foreach (var service in Services) {
Controller.ServiceName=service;
if (Controller.Status == ServiceControllerStatus.Stopped) {
Console.WriteLine("{0} was not running.", Controller.DisplayName);
}else {
Console.WriteLine("Stopping {0} on {1}", Controller.DisplayName, Controller.MachineName);
try {
Controller.Stop();
Controller.WaitForStatus(ServiceControllerStatus.Stopped);
} catch (InvalidOperationException) {
Console.WriteLine("Could not stop {0}", Controller.DisplayName);
}
Console.WriteLine("Service {0} now set to {1}", Controller.DisplayName, Controller.Status);
}
}
Console.WriteLine("\nDeleting Log Files on " + Controller.MachineName + " ...");
DeleteLogFiles();
}
private static void DeleteLogFiles()
{
foreach (var file in
Paths.SelectMany(path => FormatsToDelete, Directory.GetFiles).SelectMany(fileList => fileList)) {
try {
File.Delete(file);
}catch(IOException io) {
Console.WriteLine("\n" + io.Message);
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的所有服务在停止时是否都对正在写入的文件调用关闭?
Are all of your Services calling Close on the files they're writing to when they are Stop'ped?
Conrad Frix 是对的,尽管服务停止了,但进程仍在运行并且没有释放文件。
Conrad Frix was right, although the service stopped the process was still running and did not release the file.