c# - 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();
}
}
}
任何帮助表示赞赏。
解决方案
推荐阅读
- java - 无法将战争文件部署到 Weblogic 12c 服务器上的特定文件夹
- c# - 如何在没有列名的情况下序列化 sql 数据?
- gradle - Gradle 子项目依赖项无法由需要它的项目解决
- javascript - (JavaScript & HTML )将项目从一个列表移动到另一个列表
- azure-active-directory - 如何重置 API 权限 Microsoft Graph
- r - 如何重复加入 r 中的表?
- ios - Mapbox 中没有语音命令逐个转弯方向视图
- arrays - 如何过滤再次包含 JSON 对象数组的 JSON 对象数组?
- javascript - 创建 DOM 元素时返回的承诺中的 HTTP GET 错误
- html - 背景图像在 Safari 中未正确拉伸