【问题标题】:C#: Access denied on deleting fileC#:删除文件时拒绝访问
【发布时间】:2013-10-01 22:12:03
【问题描述】:

我有一个 c# 应用程序可以合并多个 TIFF 文件。合并文件后,我将其保存到其他位置,并删除原始 TIFF(图像文件)。但它给出了一个错误"The process cannot access the file 'D:\A\Merged.tif' because it is being used by another process."

我也使用 GC.collect() 方法来释放资源...

请帮忙,如何删除这些文件?

 int mergeTiffPages(string filepath,string[] path)
    {
        string[] sa = path;
        ImageCodecInfo info = null;
        foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders())
            if (ice.MimeType == "image/tiff")
                info = ice;
        Encoder enc = Encoder.SaveFlag;
        EncoderParameters ep = new EncoderParameters(1);
        ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
        Bitmap pages = null;
        int frame = 0;
        foreach (string s in sa){
            if (frame == 0){
                pages = (Bitmap)Image.FromFile(s);
                //save the first frame
                pages.Save(filepath, info, ep);
            }
            else{
                //save the intermediate frames
                ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
                Bitmap bm = (Bitmap)Image.FromFile(s);
                pages.SaveAdd(bm, ep);
            }
            if (frame == sa.Length - 1)
            {
                //flush and close.
                ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
                pages.SaveAdd(ep);
            }
            frame++;
        }
        return 1;
    }

【问题讨论】:

  • 你能告诉我们你是如何合并文件的吗?合并完成后要关闭文件吗?
  • 在你展示一些代码之前,你会不断地对我的朋友投反对票。
  • 显示相关代码。进行研究(即将您的异常消息粘贴到您最喜欢的网络搜索引擎中)。参见例如process cannot access the file because it is being used by another process
  • 投了赞成票,是 SO 的新手。但在未来发布问题的细节和一些代码。
  • 字符串[] sa = 路径; ImageCodecInfo 信息 = 空; foreach (ImageCodecInfo.GetImageEncoders() 中的 ImageCodecInfo ice) if (ice.MimeType == "image/tiff") info = ice;编码器 enc = Encoder.SaveFlag;编码器参数 ep = new EncoderParameters(1); ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);位图页=空; int 帧 = 0;

标签: c#


【解决方案1】:

如果可能的话,用Using {} 块包装你的代码。这是使用Using读取文件的示例代码

using System;
using System.IO;
class Test
{
   static void Main() {
      using (TextWriter w = File.CreateText("log.txt")) {
         w.WriteLine("This is line one");
         w.WriteLine("This is line two");
      }
      using (TextReader r = File.OpenText("log.txt")) {
         string s;
         while ((s = r.ReadLine()) != null) {
            Console.WriteLine(s);
         }
      }
   }
}

【讨论】:

  • @HirenRaiyani 这只是为了帮助。它不是实际的代码。
  • 我把代码放在我的问题中,请出示并给我答案
【解决方案2】:

您可能没有正确关闭图像文件。这可能是获得此异常的原因。试试下面的代码

foreach (string s in sa){
    if (string.IsNullOrEmpty(s))
    {
       continue;
    }

    using (FileStream fileStream = System.IO.File.Open(s, FileMode.Open))
    {
        if (frame == 0){
            pages = (Bitmap)Image.FromStream(fileStream);
            //save the first frame
        }
        else{
            //save the intermediate frames
        }
        if (frame == sa.Length - 1)
        {
            //flush and close.
        }
        frame++;
    }
}

【讨论】:

  • 对不起,它抛出内存不足异常
  • TIFF 文件的大小是多少?
  • 文件大小为 10 到 15 KB,但也很大,但在本例中我使用 10 到 15 KB 的文件。
  • 现在检查代码。您的某些字符串可能具有无效的文件路径。我只是在检查它们是 null 还是空。
  • 不,当 frame=1 时 else 部分控制 go 和执行 Bitmap bm = (Bitmap)Image.FromFile(s);在这一行发生错误 OutOfMemoryException was Unhandled
猜你喜欢
  • 1970-01-01
  • 2017-05-07
  • 1970-01-01
  • 2016-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-28
  • 1970-01-01
相关资源
最近更新 更多