首页 > 解决方案 > C# 无法删除文件,因为它正在被使用

问题描述

我目前正在尝试用 C# 制作一个工作跟踪器,我希望它临时创建一个文件来保存屏幕活动的屏幕截图,然后将这些屏幕截图转换为视频,然后删除所有屏幕截图。到目前为止一切正常,除了我删除屏幕截图的最后一部分,因为它说它们正在被程序使用。我不确定错误出在哪里,所以我只提供到目前为止的所有代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Imaging;
using Accord.Video.FFMPEG;
using System.IO;

namespace WorkTracker
{
    public partial class Form1 : Form
    {
        //Vars:
        string outputPath = "";
        string path = "";
        int fileCount = 1;
        List<string> inputImageSequence = new List<string>();

        //Functions:
        void takeScreenshot()
        {
            Rectangle bounds = Screen.FromControl(this).Bounds;
            using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
                }
                string name = path + "//screenshot-" + fileCount + ".jpeg";
                bitmap.Save(name, ImageFormat.Jpeg);
                inputImageSequence.Add(name);
                fileCount++;
            }
        }

        public static void DeletePath(string target_dir)
        {
            string[] files = Directory.GetFiles(target_dir);
            string[] dirs = Directory.GetDirectories(target_dir);

            foreach (string file in files)
            {
                File.SetAttributes(file, FileAttributes.Normal);
                File.Delete(file);
            }

            foreach (string dir in dirs)
            {
                DeletePath(dir);
            }

            Directory.Delete(target_dir, false);
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            tmrTrack.Start();
        }

        private void tmrTrack_Tick(object sender, EventArgs e)
        {
            takeScreenshot();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            tmrTrack.Stop();
            int width = 1920;
            int height = 1080;
            var framRate = 5;

            using (var vFWriter = new VideoFileWriter())
            {
                //Create new video file:
                vFWriter.Open(outputPath+"//video.avi", width, height, framRate, VideoCodec.Raw);

                foreach (var imageLocation in inputImageSequence)
                {
                    Bitmap imageFrame = System.Drawing.Image.FromFile(imageLocation) as Bitmap;
                    vFWriter.WriteVideoFrame(imageFrame);
                    break;
                }
                vFWriter.Close();
            }
            DeletePath(path);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
            folderBrowser.Description = "Select an Output Folder";

            if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                outputPath = @folderBrowser.SelectedPath;
            }
            else
            {
                MessageBox.Show("Please select an output folder.", "Error");
                Form1_Load(sender, e);
            }

            string pathName = "C://Documents//tempScreenCaps";
            System.IO.Directory.CreateDirectory(pathName);
            path = pathName;
        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            tmrTrack.Stop();
        }
    }
}

任何帮助表示赞赏。

标签: c#file-iodelete-file

解决方案


推荐阅读