1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.IO; 5 6 namespace RocketStudio 7 { 8 class ShredFile 9 { 10 /// <summary> 11 /// 文件粉碎选项 12 /// </summary> 13 public enum ShredMode 14 { 15 /// <summary> 16 /// 随机数 17 /// </summary> 18 RandData, 19 /// <summary> 20 /// 指定的Byte 21 /// </summary> 22 UserByte, 23 /// <summary> 24 /// 指定的字符串 25 /// </summary> 26 UserString 27 } 28 /// <summary> 29 /// 将指定的流粉碎使用随机数 30 /// </summary> 31 /// <param name="fs">文件流</param> 32 /// <returns></returns> 33 public bool Shred(FileStream fs) 34 { 35 try 36 { 37 BinaryWriter bw = new BinaryWriter(fs); 38 long fileLength = fs.Length; 39 long writeBytes = 0; 40 Random random = new Random(); 41 byte[] data = new byte[10240]; 42 random.NextBytes(data); 43 44 for (; writeBytes < fileLength; writeBytes += 10240) 45 { 46 if (fileLength - writeBytes > 10240) 47 bw.Write(data); 48 else bw.Write(data, 0, (int)(fileLength - writeBytes)); 49 } 50 fs.Flush(); 51 return true; 52 } 53 catch 54 { 55 return false; 56 } 57 } 58 /// <summary> 59 /// 将指定的流粉碎使用指定字符 60 /// </summary> 61 /// <param name="fs">文件流</param> 62 /// <param name="data">字符串</param> 63 /// <returns></returns> 64 public bool Shred(FileStream fs, byte data) 65 { 66 try 67 { 68 BinaryWriter bw = new BinaryWriter(fs); 69 long fileLength = fs.Length; 70 long writeBytes = 0; 71 for (; writeBytes < fileLength; writeBytes++) 72 { 73 bw.Write(data); 74 } 75 fs.Flush(); 76 return true; 77 } 78 catch 79 { 80 return false; 81 } 82 } 83 /// <summary> 84 /// 将指定的流粉碎使用指定字符串 85 /// </summary> 86 /// <param name="fs">文件流</param> 87 /// <param name="data">字符串</param> 88 /// <returns></returns> 89 public bool Shred(FileStream fs, string data) 90 { 91 try 92 { 93 BinaryWriter bw = new BinaryWriter(fs); 94 long fileLength = fs.Length; 95 long writeBytes = 0; 96 char[] dataArry = new char[data.Length]; 97 for (int ii = 0;ii<data.Length;ii++) 98 { 99 dataArry[ii] = data[ii]; 100 } 101 int dataLength = dataArry.Length; 102 for (; writeBytes < fileLength; writeBytes += dataLength) 103 { 104 if ((fileLength - writeBytes) < dataLength) 105 bw.Write(dataArry); 106 else bw.Write(dataArry, 0, dataLength); 107 } 108 fs.Flush(); 109 return true; 110 } 111 catch 112 { 113 return false; 114 } 115 } 116 public void RenameFile(string old, string last) 117 { 118 File.Move(old, last); 119 } 120 /// <summary> 121 /// 使用随机数生成文件名 122 /// </summary> 123 /// <returns></returns> 124 private string ReandName() 125 { 126 Random random = new Random(); 127 random.Next(1000).ToString(); 128 return random.Next(1000).ToString() + random.Next(1000).ToString() + random.Next(1000).ToString(); 129 } 130 /// <summary> 131 /// 开始粉碎 132 /// </summary> 133 /// <param name="fileName">文件名</param> 134 /// <param name="data">填充数据,ShredMode.RandData 将忽略 可为NULL</param> 135 /// <param name="sm">粉碎文件模式</param> 136 /// <returns>是否成功</returns> 137 public bool StartShredFile(string fileName, object data, ShredMode sm, bool IsCleanCreatedDate, bool IsCleanLastUsedDate, bool IsCleanName, bool IsEmptyFile) 138 { 139 FileStream fs = null; 140 try 141 { 142 fs = File.Open(fileName, FileMode.Open); 143 } 144 catch 145 { 146 //error 147 return false; 148 } 149 bool IsSucceed = false; 150 switch (sm) 151 { 152 case ShredMode.RandData: 153 IsSucceed = Shred(fs); 154 break; 155 case ShredMode.UserByte: 156 IsSucceed = Shred(fs, Convert.ToByte(data)); 157 break; 158 case ShredMode.UserString: 159 IsSucceed = Shred(fs, (string)data); 160 break; 161 162 } 163 DateTime dt = new DateTime(2012,12,24); 164 try 165 { 166 FileInfo fi = new FileInfo(fileName); 167 string randName; 168 if (IsCleanName) 169 randName = ReandName(); 170 else randName = @"\"+fi.Name; 171 172 fs.Close(); 173 if(IsEmptyFile) 174 File.Open(fi.FullName, FileMode.Truncate).Close(); 175 if(IsCleanName) 176 RenameFile(fi.FullName, fi.DirectoryName + randName); 177 if(IsCleanCreatedDate) 178 File.SetCreationTime(fi.DirectoryName + randName, dt); 179 if (IsCleanLastUsedDate) 180 { 181 if(IsCleanName) 182 File.SetLastWriteTime(fi.DirectoryName + randName, dt); 183 else 184 File.SetLastWriteTime(fi.FullName, dt); 185 } 186 File.Delete(fi.DirectoryName + randName); 187 } 188 189 catch 190 { 191 return false; 192 } 193 finally { fs.Close(); } 194 return IsSucceed; 195 } 196 } 197 198 }View Code
bool StartShredFile(string fileName, object data, ShredMode sm, bool IsCleanCreatedDate, bool IsCleanLastUsedDate, bool IsCleanName, bool IsEmptyFile)
>>>使用指定方法粉碎文件并删除
string filename : 文件名
ShredMode sm : 粉碎的方法(填充数据的类型)(RandData随机数, UserByte指定的字节, UserString指定的字符串)
Object data : 填充的数据(根据ShredMode指定的方法拆箱)可以为string,byte,在ShredMode为RandData时可以为null
bool IsCleanCreatedDate : 是否更改创建日期(指定为2012,12,24)
bool IsCleanLastUsedDate : 是否更改最后使用日期(同上)
返回是否成功
bool Shred(FileStream fs)内部方法>>>填充文件(没来得及换名字,也算粉碎吧) 重载 0
将FileStream fs指定的文件填充为随机数
返回是否成功
bool Shred(FileStream fs, byte data)内部方法>>>填充文件(没来得及换名字,也算粉碎吧) 重载 1
将FileStream fs指定的文件填充为byte data 的数据
返回是否成功
bool Shred(FileStream fs, string data)内部方法>>>填充文件(没来得及换名字,也算粉碎吧) 重载 2
将FileStream fs指定的文件填充为string data 的数据
返回是否成功
枚举 enum ShredMode 文件粉碎的方法 随机数
RandData,
指定的Byte
UserByte,
指定的字符串
UserString
引用:http://www.cnblogs.com/1119242459blog/archive/2012/01/29/2331019.html