删除文件时我的代码出错? [重复]

时间:2022-01-07 20:22:52

This question already has an answer here:

这个问题在这里已有答案:

I edit pic file in path and create new image file for there.my code is:

我在路径中编辑pic文件并为其创建新的图像文件。我的代码是:

string[] files = Directory.GetFiles(string.Concat(Server.MapPath("/"), "tmp/"));
foreach (string path in files)
{
    string filename = Path.GetFileName(path);
    using (Bitmap b = new Bitmap(string.Concat(Server.MapPath("/"), "tmp/", filename)))
    {

        SolidBrush pixelBrush = new SolidBrush(Color.White);
        Graphics g = Graphics.FromImage(b);
        g.FillRectangle(Brushes.White, 0, 0, 105, 40);

        string outputFileName = string.Concat(Server.MapPath("/"), "tmp\\E", filename);
        MemoryStream memory = new MemoryStream();
        FileStream fs = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite);
        b.Save(memory, ImageFormat.Jpeg);
        byte[] bytes = memory.ToArray();
        fs.Write(bytes, 0, bytes.Length);
        fs.Close();
        memory.Close();
        b.Dispose();
    }
    File.Delete(path);
}

when delete old file error happend is:

删除旧文件时​​发生的错误是:

Additional information: The process cannot access the file 'G:\project\WebApplication1\WebApplication1\tmp\b381ae6.jpg' because it is being used by another process.

附加信息:进程无法访问文件'G:\ project \ WebApplication1 \ WebApplication1 \ tmp \ b381ae6.jpg',因为它正由另一个进程使用。

how to fix it?

怎么解决?

3 个解决方案

#1


1  

Wrapping Graphics with using will fix it. You should dispose it also.

使用包装图形将修复它。你也应该处理它。

            using (Bitmap b = new Bitmap(filePath))
            {
                using (Graphics g = Graphics.FromImage(b))
                {
                    ...
                }
            }

Also you can use using statements by combining them.

您也可以通过组合使用using语句。

        foreach (var path in files)
        {
            var filename = Path.GetFileName(path);
            var filePath = string.Concat(tmpPath, filename);
            using (var b = new Bitmap(filePath))
            using (var g = Graphics.FromImage(b))
            {
                g.FillRectangle(Brushes.White, 0, 0, 105, 40);
                var outputFileName = string.Concat(newPath, filename);

                using (var memory = new MemoryStream())
                using (var fs = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite))
                {
                    b.Save(memory, ImageFormat.Jpeg);
                    var bytes = memory.ToArray();
                    fs.Write(bytes, 0, bytes.Length);
                }
            }
            File.Delete(path);
        }

#2


0  

write code in using scope and check File exists

使用范围编写代码并检查文件是否存在

Example

 using (var b = new Bitmap(filePath))
            using (var g = Graphics.FromImage(b))

  using (var memory = new MemoryStream())
                using (var fs = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite))

bool exists = File.Exists("Path");

if(exists)
{
  // Code Here 
}

#3


-1  

You need to close the filestream before doing other operations on that file ;)

在对该文件执行其他操作之前,您需要关闭文件流;)

#1


1  

Wrapping Graphics with using will fix it. You should dispose it also.

使用包装图形将修复它。你也应该处理它。

            using (Bitmap b = new Bitmap(filePath))
            {
                using (Graphics g = Graphics.FromImage(b))
                {
                    ...
                }
            }

Also you can use using statements by combining them.

您也可以通过组合使用using语句。

        foreach (var path in files)
        {
            var filename = Path.GetFileName(path);
            var filePath = string.Concat(tmpPath, filename);
            using (var b = new Bitmap(filePath))
            using (var g = Graphics.FromImage(b))
            {
                g.FillRectangle(Brushes.White, 0, 0, 105, 40);
                var outputFileName = string.Concat(newPath, filename);

                using (var memory = new MemoryStream())
                using (var fs = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite))
                {
                    b.Save(memory, ImageFormat.Jpeg);
                    var bytes = memory.ToArray();
                    fs.Write(bytes, 0, bytes.Length);
                }
            }
            File.Delete(path);
        }

#2


0  

write code in using scope and check File exists

使用范围编写代码并检查文件是否存在

Example

 using (var b = new Bitmap(filePath))
            using (var g = Graphics.FromImage(b))

  using (var memory = new MemoryStream())
                using (var fs = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite))

bool exists = File.Exists("Path");

if(exists)
{
  // Code Here 
}

#3


-1  

You need to close the filestream before doing other operations on that file ;)

在对该文件执行其他操作之前,您需要关闭文件流;)

相关文章