I have a WCF service serving a pdf to a WPF client. The Client opens the pdf in a new WPF window and displays it in a WebBrowser element. The Service returns the pdf as a memorystream and the client display window copies the memorystream to a filestream. I want to be able to close the display window and open a new one with a different selected pdf. After returning the first pdf I can no longer open a new one because the original pdf file is attached to a process. 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. 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
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
Use using
block for Stream
and FileStream
.
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. Let ServiceCardBrowser
should handle how it should operate on the specified file path.
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
. Now on Window_Closing
, you should check that how to set the browser source to null. If ServiceCardBrowser
is control then it can set as
ServiceCardBrowser.Source = null;
Or wrapper then check how it can the Source can be reset.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.