C#无法删除文件,因为正在使用它

时间:2018-11-25 21:04:37

标签: c# file-io delete-file

我目前正在尝试使用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();
        }
    }
}

感谢您的帮助。

0 个答案:

没有答案