zoukankan      html  css  js  c++  java
  • c# 删除程序占用的文件,强力删除文件,彻底删除文件,解除文件占用

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


  • 相关阅读:
    关于vivado----xdc文件时钟约束的初识
    Xilinx FPGA开发环境vivado使用流程
    GTX_SDI搭建流程
    SDI core端口说明
    Vivado中ILA的使用
    SDI初识
    BeautifulSoup库children(),descendants()方法的使用
    阅读OReilly.Web.Scraping.with.Python.2015.6笔记---BeautifulSoup---findAll
    初次使用CentOs7遇到的问题
    Centos7安装WPS和截图工具shutter
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/7093600.html
Copyright © 2011-2022 走看看