一.DotNet文件目录常用操作:
DiveInfo:提供了对逻辑磁盘的基本信息访问的途径。(只能查看信息,不能做任何修改。)
System.Environment:用来枚举驱动器。(不能获取驱动器的属性)
System.Management:.NET针对WMI调用。
(WMI,是Windows 2K/XP管理系统的核心;对于其他的Win32操作系统,WMI是一个有用的插件。WMI以CIMOM为基础,CIMOM即公共信息模型对象管理器(Common Information Model Object Manager),是一个描述操作系统构成单元的对象数据库,为MMC和脚本程序提供了一个访问操作系统构成单元的公共接口。有了WMI,工具软件和脚本程序访问操作系统的不同部分时不需要使用不同的API;相反,操作系统的不同部分都可以插入WMI,如图所示,工具软件和脚本程序可以方便地读写WMI。)
Directory和DircetoryInfo:用于操作目录。(前者为静态类,后者则须在实例化后调用,功能上相同)
File和FileInfo:用于操作文件。(前者为静态类,后者须实例化后调用,功能上相同)
以上介绍了一些文件的基本操作类,本次主要讲解目录和文件操作,一下给出文件和目录操作的一些基本方法
(1).文件操作:
/// <summary> /// 写文件 /// </summary> /// <param name="fileName">文件名</param> /// <param name="content">文件内容</param> /// <param name="encoding">指定文件编码</param> protected void Write_Txt(string fileName, string content, string encoding) { if (string.IsNullOrEmpty(fileName)) { throw new ArgumentNullException(fileName); } if (string.IsNullOrEmpty(content)) { throw new ArgumentNullException(content); } if (string.IsNullOrEmpty(encoding)) { throw new ArgumentNullException(encoding); } var code = Encoding.GetEncoding(encoding); var htmlfilename = HttpContext.Current.Server.MapPath("Precious\\" + fileName + ".txt"); var str = content; var sw = StreamWriter.Null; try { using (sw = new StreamWriter(htmlfilename, false, code)) { sw.Write(str); sw.Flush(); } } catch (IOException ioex) { throw new IOException(ioex.Message); } catch (Exception ex) { throw new Exception(ex.Message); } finally { sw.Close(); } } /// <summary> /// 读文件 /// </summary> /// <param name="filename">文件路径</param> /// <param name="encoding">文件编码</param> /// <returns></returns> protected string Read_Txt(string filename, string encoding) { if (string.IsNullOrEmpty(filename)) { throw new ArgumentNullException(filename); } if (string.IsNullOrEmpty(encoding)) { throw new ArgumentNullException(encoding); } var code = Encoding.GetEncoding(encoding); var temp = HttpContext.Current.Server.MapPath("Precious\\" + filename + ".txt"); var str = string.Empty; if (!System.IO.File.Exists(temp)) return str; var sr = StreamReader.Null; try { using (sr = new StreamReader(temp, code)) { str = sr.ReadToEnd(); } } catch (IOException ioex) { throw new IOException(ioex.Message); } catch (Exception ex) { throw new Exception(ex.Message); } finally { sr.Close(); } return str; } /// <summary> /// 拷贝文件 /// </summary> /// <param name="orignFile">原始文件</param> /// <param name="newFile">新文件路径</param> public static void FileCoppy(string orignFile, string newFile) { if (string.IsNullOrEmpty(orignFile)) { throw new ArgumentException(orignFile); } if (string.IsNullOrEmpty(newFile)) { throw new ArgumentException(newFile); } System.IO.File.Copy(orignFile, newFile, true); } /// <summary> /// 删除文件 /// </summary> /// <param name="path">路径</param> public static void FileDel(string path) { if (string.IsNullOrEmpty(path)) { throw new ArgumentException(path); } System.IO.File.Delete(path); } /// <summary> /// 移动文件 /// </summary> /// <param name="orignFile">原始路径</param> /// <param name="newFile">新路径</param> public static void FileMove(string orignFile, string newFile) { if (string.IsNullOrEmpty(orignFile)) { throw new ArgumentException(orignFile); } if (string.IsNullOrEmpty(newFile)) { throw new ArgumentException(newFile); } System.IO.File.Move(orignFile, newFile); }
2.目录操作:
/// <summary> /// 在当前目录下创建目录 /// </summary> /// <param name="orignFolder">当前目录</param> /// <param name="newFloder">新目录</param> public static void FolderCreate(string orignFolder, string newFloder) { if (string.IsNullOrEmpty(orignFolder)) { throw new ArgumentException(orignFolder); } if (string.IsNullOrEmpty(newFloder)) { throw new ArgumentException(newFloder); } Directory.SetCurrentDirectory(orignFolder); Directory.CreateDirectory(newFloder); } /// <summary> /// 创建文件夹 /// </summary> /// <param name="path"></param> public static void FolderCreate(string path) { if (string.IsNullOrEmpty(path)) { throw new ArgumentException(path); } if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } } public static void FileCreate(string path) { if (string.IsNullOrEmpty(path)) { throw new ArgumentException(path); } var createFile = new FileInfo(path); if (createFile.Exists) return; var fs = createFile.Create(); fs.Close(); fs.Dispose(); } /// <summary> /// 递归删除文件夹目录及文件 /// </summary> /// <param name="dir"></param> /// <returns></returns> public static void DeleteFolder(string dir) { if (string.IsNullOrEmpty(dir)) { throw new ArgumentException(dir); } if (!Directory.Exists(dir)) return; foreach (var d in Directory.GetFileSystemEntries(dir)) { if (System.IO.File.Exists(d)) { //直接删除其中的文件 System.IO.File.Delete(d); } else { //递归删除子文件夹 DeleteFolder(d); } } //删除已空文件夹 Directory.Delete(dir, true); } /// <summary> /// 指定文件夹下面的所有内容copy到目标文件夹下面 /// </summary> /// <param name="srcPath">原始路径</param> /// <param name="aimPath">目标文件夹</param> public static void CopyDir(string srcPath, string aimPath) { if (string.IsNullOrEmpty(srcPath)) { throw new ArgumentNullException(srcPath); } if (string.IsNullOrEmpty(aimPath)) { throw new ArgumentNullException(aimPath); } try { if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar) { aimPath += Path.DirectorySeparatorChar; } if (!Directory.Exists(aimPath)) { Directory.CreateDirectory(aimPath); } var fileList = Directory.GetFileSystemEntries(srcPath); foreach (var file in fileList) { if (Directory.Exists(file)) { CopyDir(file, aimPath + Path.GetFileName(file)); } else { System.IO.File.Copy(file, aimPath + Path.GetFileName(file), true); } } } catch (IOException ioex) { throw new IOException(ioex.Message); } catch (Exception ee) { throw new Exception(ee.ToString()); } } /// <summary> /// 获取指定文件夹下所有子目录及文件 /// </summary> /// <param name="path">详细路径</param> public static string GetFoldAll(string path) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(path); } var str =string.Empty; var thisOne = new DirectoryInfo(path); str = ListTreeShow(thisOne, 0, str); return str; } /// <summary> /// 获取指定文件夹下所有子目录及文件函数 /// </summary> /// <param name="theDir">指定目录</param> /// <param name="nLevel">默认起始值,调用时,一般为0</param> /// <param name="rn">用于迭加的传入值,一般为空</param> /// <returns></returns> public static string ListTreeShow(DirectoryInfo theDir, int nLevel, string rn) { if (theDir == null) { throw new ArgumentNullException("theDir"); } //获得目录 DirectoryInfo[] subDirectories = theDir.GetDirectories(); foreach (DirectoryInfo dirinfo in subDirectories) { if (nLevel == 0) { rn += "├"; } else { var s =string.Empty; for (int i = 1; i <= nLevel; i++) { s += "│ "; } rn += s + "├"; } rn += "<b>" + dirinfo.Name + "</b><br />"; //目录下的文件 var fileInfo = dirinfo.GetFiles(); foreach (FileInfo fInfo in fileInfo) { if (nLevel == 0) { rn += "│ ├"; } else { var f = string.Empty; for (int i = 1; i <= nLevel; i++) { f += "│ "; } rn += f + "│ ├"; } rn += fInfo.Name.ToString() + " <br />"; } rn = ListTreeShow(dirinfo, nLevel + 1, rn); } return rn; } /// <summary> /// 获取指定文件夹下所有子目录及文件(下拉框形) /// </summary> /// <param name="path">详细路径</param> ///<param name="dropName">下拉列表名称</param> ///<param name="tplPath">默认选择模板名称</param> public static string GetFoldAll(string path, string dropName, string tplPath) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(path); } if (string.IsNullOrEmpty(tplPath)) { throw new ArgumentNullException(tplPath); } var strDrop = "<select name=\"" + dropName + "\" id=\"" + dropName + "\"><option value=\"\">--请选择详细模板--</option>"; var str =string.Empty; DirectoryInfo thisOne = new DirectoryInfo(path); str = ListTreeShow(thisOne, 0, str, tplPath); return strDrop + str + "</select>"; } /// <summary> /// 获取指定文件夹下所有子目录及文件函数 /// </summary> /// <param name="theDir">指定目录</param> /// <param name="nLevel">默认起始值,调用时,一般为0</param> /// <param name="rn">用于迭加的传入值,一般为空</param> /// <param name="tplPath">默认选择模板名称</param> /// <returns></returns> public static string ListTreeShow(DirectoryInfo theDir, int nLevel, string rn, string tplPath) { if (theDir == null) { throw new ArgumentNullException("theDir"); } //获得目录 DirectoryInfo[] subDirectories = theDir.GetDirectories(); foreach (DirectoryInfo dirinfo in subDirectories) { rn += "<option value=\"" + dirinfo.Name + "\""; if (string.Equals(tplPath, dirinfo.Name, StringComparison.CurrentCultureIgnoreCase)) { rn += " selected "; } rn += ">"; if (nLevel == 0) { rn += "┣"; } else { string s = string.Empty; for (int i = 1; i <= nLevel; i++) { s += "│ "; } rn += s + "┣"; } rn += "" + dirinfo.Name + "</option>"; //目录下的文件 FileInfo[] fileInfo = dirinfo.GetFiles(); foreach (FileInfo fInfo in fileInfo) { rn += "<option value=\"" + dirinfo.Name + "/" + fInfo.Name + "\""; if (string.Equals(tplPath, fInfo.Name, StringComparison.CurrentCultureIgnoreCase)) { rn += " selected "; } rn += ">"; if (nLevel == 0) { rn += "│ ├"; } else { string f = string.Empty; for (int i = 1; i <= nLevel; i++) { f += "│ "; } rn += f + "│ ├"; } rn += fInfo.Name + "</option>"; } rn = ListTreeShow(dirinfo, nLevel + 1, rn, tplPath); } return rn; } /// <summary> /// 获取文件夹大小 /// </summary> /// <param name="dirPath">文件夹路径</param> /// <returns></returns> public static long GetDirectoryLength(string dirPath) { if (string.IsNullOrEmpty(dirPath)) { throw new ArgumentNullException(dirPath); } if (!Directory.Exists(dirPath)) { return 0; } long len = 0; DirectoryInfo di = new DirectoryInfo(dirPath); foreach (FileInfo fi in di.GetFiles()) { len += fi.Length; } DirectoryInfo[] dis = di.GetDirectories(); if (dis.Length > 0) { for (int i = 0; i < dis.Length; i++) { len += GetDirectoryLength(dis[i].FullName); } } return len; } /// <summary> /// 获取指定文件详细属性 /// </summary> /// <param name="filePath">文件详细路径</param> /// <returns></returns> public static string GetFileAttibe(string filePath) { if (string.IsNullOrEmpty(filePath)) { throw new ArgumentNullException(filePath); } var str = string.Empty; FileInfo objFi = new FileInfo(filePath); str += "详细路径:" + objFi.FullName + "<br>文件名称:" + objFi.Name + "<br>文件长度:" + objFi.Length + "字节<br>创建时间" + objFi.CreationTime.ToString() + "<br>最后访问时间:" + objFi.LastAccessTime.ToString() + "<br>修改时间:" + objFi.LastWriteTime.ToString() + "<br>所在目录:" + objFi.DirectoryName + "<br>扩展名:" + objFi.Extension; return str; }
(1).FileStream类GetAccessControl():检索文件的安全对象:
[SecuritySafeCritical] public FileSecurity GetAccessControl() { if (this._handle.IsClosed) { __Error.FileNotOpen(); } return new FileSecurity(this._handle, this._fileName, AccessControlSections.Group | AccessControlSections.Owner | AccessControlSections.Access); } [SecurityCritical, SecurityPermission(SecurityAction.Assert, UnmanagedCode=true)] internal FileSecurity(SafeFileHandle handle, string fullPath, AccessControlSections includeSections) : base(false, handle, includeSections, false) { if (fullPath != null) { new FileIOPermission(FileIOPermissionAccess.NoAccess, AccessControlActions.View, fullPath).Demand(); } else { new FileIOPermission(PermissionState.Unrestricted).Demand(); } }
(2).FileStream类SetAccessControl():保存设置。
[SecuritySafeCritical] public void SetAccessControl(FileSecurity fileSecurity) { if (fileSecurity == null) { throw new ArgumentNullException("fileSecurity"); } if (this._handle.IsClosed) { __Error.FileNotOpen(); } fileSecurity.Persist(this._handle, this._fileName); }
3.文件共享操作实例:
/// <summary> /// 共享文档操作 /// </summary> public class FileSharingOperationHelper { public static bool ConnectState(string path) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(path); } return ConnectState(path, "", ""); } /// <summary> /// 连接远程共享文件夹 /// </summary> /// <param name="path">远程共享文件夹的路径</param> /// <param name="userName">用户名</param> /// <param name="passWord">密码</param> /// <returns></returns> public static bool ConnectState(string path, string userName, string passWord) { var proc = new Process(); try { proc.StartInfo.FileName = "cmd.exe"; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardInput = true; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.CreateNoWindow = true; proc.Start(); var dosLine = "net use " + path + " " + passWord + " /user:" + userName; proc.StandardInput.WriteLine(dosLine); proc.StandardInput.WriteLine("exit"); while (!proc.HasExited) { proc.WaitForExit(1000); } var errormsg = proc.StandardError.ReadToEnd(); proc.StandardError.Close(); if (!string.IsNullOrEmpty(errormsg)) { throw new Exception(errormsg); } } catch (Exception ex) { throw new Exception(ex.Message); } finally { proc.Close(); proc.Dispose(); } return true; } /// <summary> /// 向远程文件夹保存本地内容,或者从远程文件夹下载文件到本地 /// </summary> /// <param name="src">要保存的文件的路径,如果保存文件到共享文件夹,这个路径就是本地文件路径如:@"D:\1.avi"</param> /// <param name="dst">保存文件的路径,不含名称及扩展名</param> /// <param name="fileName">保存文件的名称以及扩展名</param> public static void Transport(string src, string dst, string fileName) { if (string.IsNullOrEmpty(src)) { throw new ArgumentNullException(src); } if (string.IsNullOrEmpty(dst)) { throw new ArgumentNullException(dst); } if (string.IsNullOrEmpty(fileName)) { throw new ArgumentNullException(fileName); } FileStream inFileStream = null; FileStream outFileStream = null; try { inFileStream = new FileStream(src, FileMode.Open); if (!Directory.Exists(dst)) { Directory.CreateDirectory(dst); } dst = dst + fileName; outFileStream = new FileStream(dst, FileMode.OpenOrCreate); var buf = new byte[inFileStream.Length]; int byteCount; while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0) { outFileStream.Write(buf, 0, byteCount); } } catch (IOException ioex) { throw new IOException(ioex.Message); } catch (Exception ex) { throw new Exception(ex.Message); } finally { if (inFileStream != null) { inFileStream.Flush(); inFileStream.Close(); } if (outFileStream != null) { outFileStream.Flush(); outFileStream.Close(); } } } }
4.文件彻底删除实例:
在.NET中提供了两种文件彻底的方法:
(1).调用系统API来完成这样的“粉碎”操作。
(2).在删除文件之前先删除文件的所有内容,然后在执行删除操作,被称为“假粉碎”。(此方法可以被人恢复文件,但是恢复的数据只是文件中的0)
/// <summary> /// 粉碎文件操作 /// </summary> public class KillFileHelper { /// <summary> /// 强力粉碎文件,文件如果被打开,很难粉碎 /// </summary> /// <param name="filename">文件全路径</param> /// <param name="deleteCount">删除次数</param> /// <param name="randomData">随机数据填充文件,默认true</param> /// <param name="blanks">空白填充文件,默认false</param> /// <returns>true:粉碎成功,false:粉碎失败</returns> public static bool KillFile(string filename, int deleteCount, bool randomData = true, bool blanks = false) { if (string.IsNullOrEmpty(filename)) { throw new ArgumentNullException(filename); } const int bufferLength = 1024000; var ret = true; try { using (var stream = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) { var f = new FileInfo(filename); var count = f.Length; long offset = 0; var rowDataBuffer = new byte[bufferLength]; while (count >= 0) { var iNumOfDataRead = stream.Read(rowDataBuffer, 0, bufferLength); if (iNumOfDataRead == 0) { break; } if (randomData) { var randombyte = new Random(); randombyte.NextBytes(rowDataBuffer); } else if (blanks) { for (var i = 0; i < iNumOfDataRead; i++) rowDataBuffer[i] = 0; } else { for (var i = 0; i < iNumOfDataRead; i++) rowDataBuffer[i] = Convert.ToByte(Convert.ToChar(deleteCount)); } // 写新内容到文件。 for (var i = 0; i < deleteCount; i++) { stream.Seek(offset, SeekOrigin.Begin); stream.Write(rowDataBuffer, 0, iNumOfDataRead); } offset += iNumOfDataRead; count -= iNumOfDataRead; } } //每一个文件名字符代替随机数从0到9。 var newName = ""; do { var random = new Random(); var cleanName = Path.GetFileName(filename); var dirName = Path.GetDirectoryName(filename); var iMoreRandomLetters = random.Next(9); // 为了更安全,不要只使用原文件名的大小,添加一些随机字母。 for (var i = 0; i < cleanName.Length + iMoreRandomLetters; i++) { newName += random.Next(9).ToString(); } newName = dirName + "\\" + newName; } while (File.Exists(newName)); // 重命名文件的新的随机的名字。 File.Move(filename, newName); File.Delete(newName); } catch { //可能其他原因删除失败了,使用我们自己的方法强制删除 var matchPattern = @"(?<=\s+pid:\s+)\b(\d+)\b(?=\s+)"; try { //要检查被那个进程占用的文件 var fileName = filename; var tool = new Process { StartInfo = { FileName = "handle.exe", Arguments = fileName + " /accepteula", UseShellExecute = false, RedirectStandardOutput = true } }; tool.Start(); tool.WaitForExit(); var outputTool = tool.StandardOutput.ReadToEnd(); foreach (Match match in Regex.Matches(outputTool, matchPattern)) { //结束掉所有正在使用这个文件的程序 Process.GetProcessById(int.Parse(match.Value)).Kill(); } File.Delete(fileName); } catch { ret = false; } } return ret; } }
5.DotNet文件加密解密操作:
File和FileInfo类对文件加密进行了进一步的封装,提供了Encrypt和Decrypt方法用来对文件加密和解密。这两种方法要求文件系统必须为NFTS系统,对操作系统版本也要求必须是NT以上版本,使用该方法加密的文件,必须由同一用户才能进行解密。
(1).Encrypt():文件加密操作。
[SecuritySafeCritical] public static void Encrypt(string path) { if (path == null) { throw new ArgumentNullException("path"); } string fullPathInternal = Path.GetFullPathInternal(path); new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, new string[] { fullPathInternal }, false, false).Demand(); if (!Win32Native.EncryptFile(fullPathInternal)) { int errorCode = Marshal.GetLastWin32Error(); if (errorCode == 5) { DriveInfo info = new DriveInfo(Path.GetPathRoot(fullPathInternal)); if (!string.Equals("NTFS", info.DriveFormat)) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_EncryptionNeedsNTFS")); } } __Error.WinIOError(errorCode, fullPathInternal); } }
(2).Decrypt():文件解密操作。
[SecuritySafeCritical] public static void Decrypt(string path) { if (path == null) { throw new ArgumentNullException("path"); } string fullPathInternal = Path.GetFullPathInternal(path); new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, new string[] { fullPathInternal }, false, false).Demand(); if (!Win32Native.DecryptFile(fullPathInternal, 0)) { int errorCode = Marshal.GetLastWin32Error(); if (errorCode == 5) { DriveInfo info = new DriveInfo(Path.GetPathRoot(fullPathInternal)); if (!string.Equals("NTFS", info.DriveFormat)) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_EncryptionNeedsNTFS")); } } __Error.WinIOError(errorCode, fullPathInternal); } }