C# 删除或修改文件时提示文件被占用

C# 中使用Image.FromFile(string path)后,提示该文件正在被另一进程使用XXX的问题,是因为对应的文件在一直调用 ,其生成的Image对象被Disponse()前都不会被解除锁定,这就造成了此问题,就是在这个图形被解锁前无法对图像进行操作(比如删除,修改等操作)。

解决方案:此问题可以使用下面三个方法解决问题.

方法1:在要进行文件操作前将Image对象销毁.

System.Drawing.Image image = System.Drawing.Image.FromFile(filePath);
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(image);
image.Dispose();

方法2:在加载图像的时候用一种方法替代

System.Drawing.Image img = System.Drawing.Image.FromFile(filepath);
System.Drawing.Image bmp = new System.Drawing.Bitmap(img.Width, img.Height, img.PixelFormat);

System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);
g.DrawImage(img, 0, 0);
g.Flush();
g.Dispose();
img.Dispose();

方法3:

System.IO.FileStream fs = new System.IO.FileStream(filePath, FileMode.Open, FileAccess.Read);

int byteLength = (int)fs.Length;
byte[] fileBytes = new byte[byteLength];
fs.Read(fileBytes, 0, byteLength);

//文件流关閉,文件解除锁定
fs.Close(); MemoryStream mStream = new MemoryStream(fileBytes);
Image image = Image.FromStream(mStream);
mStream.Close();
//因为FromStream方法参数应用的流必须一直保持打开,故而代码中有一个文件流是向MemeoryStream流的转换,从而可以关闭文件流,保持MemoryStream流的打开状态。在不用mStream时关闭。

2 文件被占用强制删除类(没试过)

public void WipeFile(string filename, int timesToWrite)
{
try
{
if (File.Exists(filename))
{
//设置文件的属性为正常,这是为了防止文件是只读
File.SetAttributes(filename, FileAttributes.Normal);
//计算扇区数目
double sectors = Math.Ceiling(new FileInfo(filename).Length / 512.0);
// 创建一个同样大小的虚拟缓存
byte[] dummyBuffer = new byte[512];
// 创建一个加密随机数目生成器
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
// 打开这个文件的FileStream
FileStream inputStream = new FileStream(filename, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
for (int currentPass = 0; currentPass < timesToWrite; currentPass++)
{
// 文件流位置
inputStream.Position = 0;
//循环所有的扇区
for (int sectorsWritten = 0; sectorsWritten < sectors; sectorsWritten++)
{
//把垃圾数据填充到流中
rng.GetBytes(dummyBuffer);
// 写入文件流中
inputStream.Write(dummyBuffer, 0, dummyBuffer.Length);
}
}
// 清空文件
inputStream.SetLength(0);
// 关闭文件流
inputStream.Close();
// 清空原始日期需要
DateTime dt = new DateTime(2037, 1, 1, 0, 0, 0);
File.SetCreationTime(filename, dt);
File.SetLastAccessTime(filename, dt);
File.SetLastWriteTime(filename, dt);
// 删除文件
File.Delete(filename);
}
}
catch (Exception)
{
}
}

3 文件被另一进程占用string fileName = @“c:\aaa.doc”;//要检查被那个进程占用的文件

Process tool = new Process();
tool.StartInfo.FileName = “handle.exe”;//占用文件的进程
tool.StartInfo.Arguments = fileName+" /accepteula";
tool.StartInfo.UseShellExecute = false;
tool.StartInfo.RedirectStandardOutput = true;
tool.Start();
tool.WaitForExit();
string outputTool = tool.StandardOutput.ReadToEnd();

string matchPattern = @"(?<=\s+pid:\s+)\b(\d+)\b(?=\s+)";
foreach(Match match in Regex.Matches(outputTool, matchPattern))
{
Process.GetProcessById(int.Parse(match.Value)).Kill();
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值