c# 强制删除文件


 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();
            }

 

c# 删除程序占用的文件,清理删除文件,彻底删除文件,解除文件占用

    文件打开时,以    共享读写模式    打开   

FileStream inputStream = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); p.BackgroundImage = new Bitmap(inputStream); inputStream.Dispose();

删除文件

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) { } }
 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM