简体   繁体   中英

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

I am using SaveFileDialog to save a pdf file.But at the creation of pdf file i am getting this error.

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

Here is my code in 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();
        }
    }

I am getting this error at this line..

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

Here is my pdf file code

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

Please help me. Thanks in advance.

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

Here you open the file and then try to create it:

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

Is myStream closed inside GetInstance() method?
It is not. Otherwise you could not read from it inside using block:

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

You should either read all the contents of the file, close the stream and only after that recreate the file.
Another approach is to use temporary file to copy its contents.

Line by line:

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

What you should really do:

  • Load PDF file. ( Document probably contains static method Load / LoadFrom ore something like that.
  • Change PDF like you do inside get instance.
  • Save to file whatever you like. ( saveFileDialog1.FileName )

you are writig to the same file you are reading from. Hence the error message.

Because at the time your FileStream is instantiated, your (myStream = saveFileDialog1.OpenFile()) already has a handle open to the file.

You should use a single StreamWriter for this instead of messing around with mutliple streams.

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.

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