首页 > 解决方案 > 打印后删除文件

问题描述

我正在使用以下代码在临时路径上创建一个文件,然后在打印成功后将其删除。但是在打印后我尝试处理文件,当我尝试删除文件时,我仍然得到一个异常说“进程无法访问文件'Chart0.png',因为它正在被另一个进程使用。” 请帮忙。

我还尝试将删除代码放在 finally 块中,但仍然没有运气。

public static bool PrintAllCharts(Dictionary<string, ILightningChartInterface> charts)
        {
            DirectoryInfo info = null;
            try
            {
                string FilePathWithoutFileName = string.Empty;
                info = Directory.CreateDirectory(@"C:\TempCharts");
                for (int i = 0; i < charts.Count; i++)
                {
                    KeyValuePair<string, ILightningChartInterface> kp = charts.ElementAt(i);
                    FilePathWithoutFileName = info.FullName;
                    string FullPath = string.Format("{0}/Chart{1}.png", FilePathWithoutFileName, i.ToString());
                    kp.Value.SaveChartToFile(FullPath);
                }
                var files = Directory.GetFiles(FilePathWithoutFileName);

                using (var pdoc = new PrintDocument())
                {
                    using (var pdi = new System.Windows.Forms.PrintDialog { Document = pdoc, UseEXDialog = true })
                    {
                        if (pdi.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            pdoc.PrinterSettings = pdi.PrinterSettings;
                            pdoc.PrintPage += Pdoc_PrintPage;
                            foreach (var file in files)
                            {
                                pdoc.DocumentName = file;
                                pdoc.Print();
                            }
                        }
                    }
                }
                //Dispose the file after printing.
                foreach(var file in files)
                {
                    Image.FromFile(file).Dispose();
                    File.Delete(file); //This line gives an exception
                }

                foreach (DirectoryInfo dir in info.GetDirectories())
                {
                    dir.Delete(true);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        private static void Pdoc_PrintPage(object sender, PrintPageEventArgs e)
        {
            string file = ((PrintDocument)sender).DocumentName;
            System.Drawing.Image img = System.Drawing.Image.FromFile(file);
            Rectangle m = e.MarginBounds;

            if ((double)img.Width / (double)img.Height > (double)m.Width / (double)m.Height) // image is wider
            {
                m.Height = (int)((double)img.Height / (double)img.Width * (double)m.Width);
            }
            else
            {
                m.Width = (int)((double)img.Width / (double)img.Height * (double)m.Height);
            }
            e.Graphics.DrawImage(img, m);
        }

标签: c#fileprocess

解决方案


问题在于你的Pdoc_PrintPage方法。您正在使用以下行来读取文件:

System.Drawing.Image img = System.Drawing.Image.FromFile(file);

状态的文档FromFile

该文件保持锁定状态,直到图像被释放。

所以你真的应该这样写你的代码,以便在你完成后处理图像(并解锁文件):

string file = ((PrintDocument)sender).DocumentName;
using (System.Drawing.Image img = System.Drawing.Image.FromFile(file))
{
    Rectangle m = e.MarginBounds;

    if ((double)img.Width / (double)img.Height > (double)m.Width / (double)m.Height) // image is wider
    {
        m.Height = (int)((double)img.Height / (double)img.Width * (double)m.Width);
    }
    else
    {
        m.Width = (int)((double)img.Width / (double)img.Height * (double)m.Height);
    }
    e.Graphics.DrawImage(img, m);
}

请注意,您必须处理相同的图像实例。您当前在删除循环中有此代码:

Image.FromFile(file).Dispose();

这只是尝试加载文件的第二个副本,然后立即处理它。完成上述更改后,您还应该从删除循环中删除此行。


推荐阅读