该进程无法访问另一个进程c#正在使用的文件

时间:2014-03-24 15:05:30

标签: c# filestream

我正在使用SaveFileDialog保存pdf文件。但在创建pdf文件时,我收到此错误。

The process cannot access the file it is being used by another process c#

这是我在c#中的代码。

SaveFileDialog saveFileDialog1 = new SaveFileDialog();

saveFileDialog1.Filter = "Pdf files (*.pdf)|*.pdf|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 0;
saveFileDialog1.RestoreDirectory = true;

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
    if ((myStream = saveFileDialog1.OpenFile()) != null)
    {
        // Code to write the stream goes here.
        PdfWriter.GetInstance(pdfDoc, myStream);
        //myStream.Close();
        using (FileStream file = new FileStream(saveFileDialog1.FileName, FileMode.Create, System.IO.FileAccess.Write, FileShare.ReadWrite))
        {
            byte[] bytes = new byte[myStream.Length];
            myStream.Read(bytes, 0, (int)myStream.Length);
            file.Write(bytes, 0, bytes.Length);
            myStream.Close();
        }
    }

我在这一行收到此错误..

using (FileStream file = new FileStream(saveFileDialog1.FileName, FileMode.Create, System.IO.FileAccess.Write, FileShare.ReadWrite))

这是我的pdf文件代码

        Stream myStream;

        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);

        using (MemoryStream stream = new MemoryStream())
        {
            pdfDoc.Open();
            pieChart.SaveImage(stream, ChartImageFormat.Png);
            iTextSharp.text.Image chartImage = iTextSharp.text.Image.GetInstance(stream.GetBuffer());
            chartImage.ScalePercent(75f);
            pdfDoc.Add(chartImage);
        }
        pdfDoc.Close();

请帮帮我。 提前谢谢。

3 个答案:

答案 0 :(得分:0)

if ((myStream = saveFileDialog1.OpenFile()) != null)

在这里打开文件,然后尝试创建它:

new FileStream(saveFileDialog1.FileName, FileMode.Create, System.IO.FileAccess.Write, FileShare.ReadWrite)

myStream是否在GetInstance()方法内关闭?
它不是。否则你无法使用块读取它:

myStream.Read(bytes, 0, (int)myStream.Length);

您应该读取文件的所有内容,关闭流,然后才重新创建文件 另一种方法是使用临时文件来复制其内容。

逐行:

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
    // open the file to read its contents
    if ((myStream = saveFileDialog1.OpenFile()) != null)
    {
        // create NEW PDF object in memory
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);

        // use memory stream for image
        using (MemoryStream stream = new MemoryStream())
        {
            // open pdf for changes
            pdfDoc.Open();
            // write image to memory stream
            pieChart.SaveImage(stream, ChartImageFormat.Png);
            // create image instance from memory stream
            iTextSharp.text.Image chartImage = iTextSharp.text.Image.GetInstance(stream.GetBuffer());
            chartImage.ScalePercent(75f);
            // add the image to pdf document
            pdfDoc.Add(chartImage);
        }
        // close document... and forget about it. all changes were made in memory
        // pdfDoc will be collected by GC.
        pdfDoc.Close();

        // try to create new file with the name as already opened by myStream - Exception here
        using (FileStream file = new FileStream(saveFileDialog1.FileName, FileMode.Create, System.IO.FileAccess.Write, FileShare.ReadWrite))
        {
            byte[] bytes = new byte[myStream.Length];
            myStream.Read(bytes, 0, (int)myStream.Length);
            file.Write(bytes, 0, bytes.Length);
            myStream.Close();
        }
    }
}

你应该做什么:

  • 加载PDF文件。 (Document可能包含静态方法Load / LoadFrom类似的东西。
  • 像在里面获取实例一样更改PDF。
  • 保存到您喜欢的任何文件。 (saveFileDialog1.FileName

答案 1 :(得分:0)

您正在写入您正在阅读的同一文件。因此错误信息。

答案 2 :(得分:0)

因为当您FileStream被实例化时,您的(myStream = saveFileDialog1.OpenFile())已经有一个对该文件开放的句柄。

你应该使用一个StreamWriter而不是乱用多个流。