繁体   English   中英

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

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

我正在使用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();

请帮我。 提前致谢。

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

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

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

GetInstance()是否在GetInstance()方法内关闭?
它不是。 否则,您将无法使用block从其中读取:

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类似的东西。
  • 像在get实例中一样更改PDF。
  • 随便保存到文件。 saveFileDialog1.FileName

您将写入与要读取的文件相同的文件。 因此,错误消息。

因为在实例化FileStream ,您的(myStream = saveFileDialog1.OpenFile())已经有一个打开文件的句柄。

您应该为此使用一个StreamWriter ,而不是搞乱多重流。

暂无
暂无

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

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