总结:
判断文件是否存在:
File.Exists("hb.txt"); //如果不指明路径,则默认为应用程序当前路径,返回值booblen型
创建文件:
File.Create("D:\\hb.txt");
复制或者移动文件:
File.Copy("C:\\hb.txt","D:\\hb.txt");
File.Move("C:\\hb.txt","D:\\hb.txt");
删除文件:
File.Delete("D:\\hb.txt");
获取文件基本信息:
FileInfo aFile=new FileInfo("D:\\hb.txt");
aFile.CreationTime //获取文件创建时间
aFile.Extension //获取文件扩展名
aFile.FullName //获取文件完整目录
aFile.Length //获取文件大小
判断文件夹是否存在:
Directory.Exists("E:\\soft"); //返回值booblen型
创建文件夹:
Directory.CreateDirectory("E:\\soft");
移动文件夹:
Directory.Move("C:\\soft","D:\\soft");
删除文件夹:
Directory.Delete("D:\\soft"); //从指定目录删除空目录
Directory.Delete("D:\\soft",True); //第二个参数为True,删除指定目录并删除目录下
//的子文件,第二个参数为False,则删除指定路径的空目录
(1)复制文件夹,连同里面所有的文件和文件夹
//filename是文件名
string path=rootpath+"\\"+"front/Album/1";
string path2=rootpath+"\\"+"front/Album/"+filename;
CommonClass.CopyDir(path, path2);
(2)删除文件夹,连同里面的文件和文件夹
string path=webpath+filename+"/";
DirectoryInfo di = new DirectoryInfo(path);
di.Delete(true);
以下是CopyDir()的函数
/// <summary>
/// 复制文件夹
/// </summary>
/// <param name="strFromDirectory">被复制的文件夹</param>
/// <param name="strToDirectory">复制后的文件夹</param>
/// <returns></returns>
public static bool CopyDir(string strFromDirectory, string strToDirectory)
{
try
{
Directory.CreateDirectory(strToDirectory);
if (!Directory.Exists(strFromDirectory)) return false;
string[] directories = Directory.GetDirectories(strFromDirectory);
if (directories.Length > 0)
{
foreach (string d in directories)
{
CopyDir(d, strToDirectory + d.Substring(d.LastIndexOf("\\")));
}
}
string[] files = Directory.GetFiles(strFromDirectory);
if (files.Length > 0)
{
foreach (string s in files)
{
File.Copy(s, strToDirectory + s.Substring(s.LastIndexOf("\\")));
}
}
return true;
}
catch (System.Exception e)
{
throw e;
}
}