.Net 最详细的文件操作移动复制删除代码

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
namespace EC
{
///<summary>
/// FileObj的摘要说明
///</summary>
public classFileObj
{
#region 构造函数
private bool _alreadyDispose = false;
public FileObj()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
~FileObj()
{
Dispose(); ;
}
protected virtual void Dispose(bool isDisposing)
{
if (_alreadyDispose) return;
//if (isDisposing)
//{
// if (xml !=null)
// {
// xml = null;
// }
//}
_alreadyDispose = true;
}
#endregion
#region IDisposable 成员
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
#region 取得文件后缀名

/// <summary>
/// 取后缀名
/// </summary>
/// <paramname="filename">文件名</param>
///<returns>.gif|.html格式</returns>
public static string GetPostfixStr(string filename)
{
int start = filename.LastIndexOf(".");
int length = filename.Length;
string postfix = filename.Substring(start, length - start);
return postfix;
}
#endregion
#region 写文件

/// <summary>
/// 写文件
/// </summary>
/// <paramname="Path">文件路径</param>
/// <paramname="Strings">文件内容</param>
public static void WriteFile(string Path, string Strings)
{
if (!System.IO.File.Exists(Path))
{
//Directory.CreateDirectory(Path);
System.IO.FileStream f = System.IO.File.Create(Path);
f.Close();
f.Dispose();
}
System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, true,System.Text.Encoding.UTF8);
f2.WriteLine(Strings);
f2.Close();
f2.Dispose();

}
#endregion
#region 读文件

/// <summary>
/// 读文件
/// </summary>
/// <paramname="Path">文件路径</param>
///<returns></returns>
public static string ReadFile(string Path)
{
string s = "";
if (!System.IO.File.Exists(Path))
s = "不存在相应的目录";
else
{
StreamReader f2 = new StreamReader(Path,System.Text.Encoding.GetEncoding("gb2312"));
s = f2.ReadToEnd();
f2.Close();
f2.Dispose();
}
return s;
}
#endregion
#region 追加文件

/// <summary>
/// 追加文件
/// </summary>
/// <paramname="Path">文件路径</param>
/// <paramname="strings">内容</param>
public static void FileAdd(string Path, string strings)
{
StreamWriter sw = File.AppendText(Path);
sw.Write(strings);
sw.Flush();
sw.Close();
sw.Dispose();
}
#endregion
#region 拷贝文件

/// <summary>
/// 拷贝文件
/// </summary>
/// <paramname="OrignFile">原始文件</param>
/// <paramname="NewFile">新文件路径</param>
public static void FileCoppy(string OrignFile, stringNewFile)
{
File.Copy(OrignFile, NewFile, true);
}
#endregion
#region 删除文件

/// <summary>
/// 删除文件
/// </summary>
/// <paramname="Path">路径</param>
public static void FileDel(string Path)
{
File.Delete(Path);
}
#endregion
#region 移动文件

/// <summary>
/// 移动文件
/// </summary>
/// <paramname="OrignFile">原始路径</param>
/// <paramname="NewFile">新路径</param>
public static void FileMove(string OrignFile, string NewFile)
{
File.Move(OrignFile, NewFile);
}
#endregion
#region 在当前目录下创建目录

/// <summary>
/// 在当前目录下创建目录
/// </summary>
/// <paramname="OrignFolder">当前目录</param>
/// <paramname="NewFloder">新目录</param>
public static void FolderCreate(string OrignFolder, stringNewFloder)
{
Directory.SetCurrentDirectory(OrignFolder);
Directory.CreateDirectory(NewFloder);
}
/// <summary>
/// 创建文件夹
/// </summary>
/// <paramname="Path"></param>
public static void FolderCreate(string Path)
{
// 判断目标目录是否存在如果不存在则新建之
if (!Directory.Exists(Path))
Directory.CreateDirectory(Path);
}
#endregion
#region 创建目录
public static void FileCreate(string Path)
{
FileInfo CreateFile = new FileInfo(Path); //创建文件
if (!CreateFile.Exists)
{
FileStream FS = CreateFile.Create();
FS.Close();
}
}
#endregion
#region 递归删除文件夹目录及文件

/// <summary>
/// 递归删除文件夹目录及文件
/// </summary>
/// <paramname="dir"></param>
///<returns></returns>
public static void DeleteFolder(string dir)
{
if (Directory.Exists(dir)) //如果存在这个文件夹删除之
{
foreach (string d in Directory.GetFileSystemEntries(dir))

你可能感兴趣的:(.net,复制,文件操作)