简体   繁体   English

如何识别和关闭在文件C#.net上运行的进程

[英]How to identify and close a process running on a file c# .net

I have a WCF service serving a pdf to a WPF client. 我有一个WCF服务,为WPF客户端提供pdf。 The Client opens the pdf in a new WPF window and displays it in a WebBrowser element. 客户端在新的WPF窗口中打开pdf并将其显示在WebBrowser元素中。 The Service returns the pdf as a memorystream and the client display window copies the memorystream to a filestream. 服务将pdf作为内存流返回,并且客户端显示窗口将内存流复制到文件流。 I want to be able to close the display window and open a new one with a different selected pdf. 我希望能够关闭显示窗口并使用其他选定的pdf打开一个新窗口。 After returning the first pdf I can no longer open a new one because the original pdf file is attached to a process. 返回第一个pdf后,我无法再打开一个新的pdf,因为原始pdf文件已附加到流程中。 I can not delete the previous file and replace it with a new file because it is attached to a process. 我无法删除先前的文件并用新文件替换它,因为它已附加到进程中。 I have tried using the filestream.dispose and filestream.close methods on my filestream, memorystream, and tried the close method on my service instance. 我尝试在文件filestream.disposefilestream.close上使用filestream.disposefilestream.close方法,并在服务实例上尝试使用close方法。 Regardless of what I have attempted I always get the same exception. 无论我尝试了什么,我总是会遇到同样的异常。 The file is attached to a process. 该文件将附加到进程。 I don't even know how to identify the process that is still attached to the file. 我什至不知道如何识别仍附加到文件的进程。 I am using Visual Studio 2017 我正在使用Visual Studio 2017

Client Side Code 客户端代码

public void DisplayCard(string SPID, string strAssetDirectory)
    {
        ServiceReference1.Service1Client proxy = new ServiceReference1.Service1Client();
        Stream ms = proxy.GetServiceCard(SPID, strAssetDirectory);
        try
        {
            using (file = new FileStream(Properties.Settings.Default.ServiceCardDisplayPath, FileMode.Create, FileAccess.Write))
            {
                ms.CopyTo(file);
                file.Close();
            }
            ms.Dispose();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        proxy.Close();
        ServiceCardBrowser.Navigate($"file://{Properties.Settings.Default.ServiceCardDisplayPath}");
    }
    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        File.Delete(Properties.Settings.Default.ServiceCardDisplayPath);
        //file.Close();
        file.Dispose();
        this.Owner.Focus();
    }

Is there a way to identify the process that is attached to the file? 有没有办法确定附加到文件的进程?

Your code need to be improved before at few places 您的代码需要在几个地方进行改进

  1. Use using block for Stream and FileStream . using块用于StreamFileStream

  2. You don't need to keep the file object as member variable. 您无需将文件对象保留为成员变量。 Even the disposed file stream you are trying to dispose again. 即使是您尝试再次处理的已处理文件流。 In terms of coding have segregation of concern. 在编码方面有关注的隔离。 Once the file is written to local directory then DisplayCard scope is finished. 将文件写入本地目录后,即可完成DisplayCard作用域。 Let ServiceCardBrowser should handle how it should operate on the specified file path. ServiceCardBrowser处理应该如何在指定的文件路径上进行操作。

     public void DisplayCard(string SPID, string strAssetDirectory) { using (ServiceReference1.Service1Client proxy = new ServiceReference1.Service1Client()) { using (Stream ms = proxy.GetServiceCard(SPID, strAssetDirectory)) { try { using (file = new FileStream(Properties.Settings.Default.ServiceCardDisplayPath, FileMode.Create, FileAccess.Write)) { ms.CopyTo(file); file.Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } } ServiceCardBrowser.Navigate( $"file://{Properties.Settings.Default.ServiceCardDisplayPath}"); } private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { File.Delete(Properties.Settings.Default.ServiceCardDisplayPath); this.Owner.Focus(); } 

The file.dispose has been removed as it was already disposed in DisplayCard . file.dispose已被删除,因为它已经被DisplayCard Now on Window_Closing , you should check that how to set the browser source to null. 现在在Window_Closing ,您应该检查如何将浏览器源设置为null。 If ServiceCardBrowser is control then it can set as 如果ServiceCardBrowser是控件,则可以将其设置为

 ServiceCardBrowser.Source = null;

Or wrapper then check how it can the Source can be reset. 或包装器,然后检查如何重置源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM