在实际的项目开发中,我们经常需要使用到文件的I/O操作,主要包含对文件的增改删查等操作,这些基本的操作我们都是很熟悉,但是较少的人去考虑文件的安全和操作的管理等方面,例如文件的访问权限管理,文件数据的彻底删除和数据的恢复等等,这一系列的操作需要我们对.NET的相关知识有一个深刻的学习。
在本文章主要介绍文件和目录的一些基本操作,以及文件目录的权限和安全设置的相关内容。
一.DotNet文件目录常用操作:
提到文件的I/O操作,这个对于每一个开发者来说都不是陌生的事,因为这些操作是我们在项目开发过程中经常使用到的一些操作。那么在.NET中操作文件的类在System.IO命名空间下,一下介绍一下常见的I/O操作类:
DiveInfo:提供了对逻辑磁盘的基本信息访问的途径。(只能查看信息,不能做任何修改。)
System.Environment:用来枚举驱动器。(不能获取驱动器的属性)
System.Management:.NET针对WMI调用。
Directory和DircetoryInfo:用于操作目录。(前者为静态类,后者则须在实例化后调用,功能上相同)
File和FileInfo:用于操作文件。(前者为静态类,后者须实例化后调用,功能上相同)
以上介绍了一些文件的基本操作类,本次主要讲解目录和文件操作,一下给出文件和目录操作的一些基本方法:
1.文件常规操作:
(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;
}
(2).文件附加操作:
/// <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 (str