【问题标题】:How to identify and close a process running on a file c# .net如何识别和关闭在文件 c# .net 上运行的进程
【发布时间】:2018-11-24 12:01:54
【问题描述】:

我有一个向 WPF 客户端提供 pdf 的 WCF 服务。客户端在新的 WPF 窗口中打开 pdf 并将其显示在 WebBrowser 元素中。服务将 pdf 作为内存流返回,客户端显示窗口将内存流复制到文件流。我希望能够关闭显示窗口并使用不同的选定 pdf 打开一个新窗口。返回第一个 pdf 后,我无法再打开新的,因为原始 pdf 文件附加到进程。我无法删除以前的文件并将其替换为新文件,因为它附加到进程。我尝试在我的文件流、内存流上使用filestream.disposefilestream.close 方法,并在我的服务实例上尝试了close 方法。不管我尝试了什么,我总是得到同样的例外。该文件附加到一个进程。我什至不知道如何识别仍然附加到文件的进程。我正在使用 Visual Studio 2017

客户端代码

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();
    }

有没有办法识别附加到文件的进程?

【问题讨论】:

标签: c# .net wpf wcf


【解决方案1】:

你的代码有几个地方需要改进

  1. StreamFileStream 使用using 块。

  2. 您不需要将文件对象保留为成员变量。即使是您尝试再次处理的已处理文件流。在编码方面有隔离的关注。将文件写入本地目录后,DisplayCard 范围就完成了。让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();
    }
    

file.dispose 已被删除,因为它已在 DisplayCard 中处理。现在在Window_Closing,您应该检查如何将浏览器源设置为空。如果ServiceCardBrowser是控制,那么它可以设置为

 ServiceCardBrowser.Source = null;

或者包装器然后检查如何重置源。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    相关资源
    最近更新 更多