c# 进程停止后无法删除日志文件

发布于 2024-09-09 17:01:42 字数 1691 浏览 3 评论 0原文

我的服务器上有许多进程,我试图停止、删除日志文件并最终重新启动。我的问题是,一旦所有进程停止(即使是写入日志文件的进程),我无法以编程方式删除日志文件(但我可以手动删除)。我收到的错误是“该进程无法访问该文件,因为它正在被另一个进程使用。即使我验证该进程已停止并将线程置于睡眠状态,我也无法删除该文件。

我尝试过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁拥 2024-09-16 17:01:42

您的所有服务在停止时是否都对正在写入的文件调用关闭?

Are all of your Services calling Close on the files they're writing to when they are Stop'ped?

似最初 2024-09-16 17:01:42

Conrad Frix 是对的,尽管服务停止了,但进程仍在运行并且没有释放文件。

Conrad Frix was right, although the service stopped the process was still running and did not release the file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文