原创
C# 文件夹操作
2009-11-9 18:38
2665
7
7
分类:
软件与OS
- 追加文件
- StreamWriter sw = File.AppendText(Server.MapPath(".")+"\\myText.txt");
- sw.WriteLine("追逐理想");
- sw.WriteLine("kzlll");
- sw.WriteLine(".NET笔记");
- sw.Flush();
- sw.Close();
- 拷贝文件
- string OrignFile,NewFile;
- OrignFile = Server.MapPath(".")+"\\myText.txt";
- NewFile = Server.MapPath(".")+"\\myTextCopy.txt";
- File.Copy(OrignFile,NewFile,true);
-
- 删除文件
- string delFile = Server.MapPath(".")+"\\myTextCopy.txt";
- File.Delete(delFile);
-
- 移动文件
- string OrignFile,NewFile;
- OrignFile = Server.MapPath(".")+"\\myText.txt";
- NewFile = Server.MapPath(".")+"\\myTextCopy.txt";
- File.Move(OrignFile,NewFile);
-
- 创建目录
-
- DirectoryInfo d=Directory.CreateDirectory("c:\\sixAge");
-
- DirectoryInfo d1=d.CreateSubdirectory("sixAge1");
-
- DirectoryInfo d2=d1.CreateSubdirectory("sixAge1_1");
-
- Directory.SetCurrentDirectory("c:\\sixAge");
-
- Directory.CreateDirectory("sixAge2");
-
- Directory.CreateDirectory("sixAge2\\sixAge2_1");
-
- 递归删除文件夹及文件
- <%@ Page Language=C#%>
- <%@ Import namespace="System.IO"%>
- <Script runat=server>
- public void DeleteFolder(string dir)
- {
- if (Directory.Exists(dir))
- {
- foreach(string d in Directory.GetFileSystemEntries(dir))
- {
- if(File.Exists(d))
- File.Delete(d);
- else
- DeleteFolder(d);
- }
- Directory.Delete(dir);
- Response.Write(dir+" 文件夹删除成功");
- }
- else
- Response.Write(dir+" 该文件夹不存在");
- }
-
- protected void Page_Load (Object sender ,EventArgs e)
- {
- string Dir="D:\\gbook\\11";
- DeleteFolder(Dir);
- }
-
-
-
-
-
-
-
- public static void CopyDir(string srcPath,string aimPath)
- {
- try
- {
-
- if(aimPath[aimPath.Length-1] != Path.DirectorySeparatorChar)
- aimPath += Path.DirectorySeparatorChar;
-
- if(!Directory.Exists(aimPath)) Directory.CreateDirectory(aimPath);
-
-
-
- string[] fileList = Directory.GetFileSystemEntries(srcPath);
-
- foreach(string file in fileList)
- {
-
- if(Directory.Exists(file))
- CopyDir(file,aimPath+Path.GetFileName(file));
-
- else
- File.Copy(file,aimPath+Path.GetFileName(file),true);
- }
- }
- catch (Exception e)
- {
- MessageBox.Show (e.ToString());
- }
- }
-
-
-
-
-
-
- public static void DeleteDir(string aimPath)
- {
- try
- {
-
- if(aimPath[aimPath.Length-1] != Path.DirectorySeparatorChar)
- aimPath += Path.DirectorySeparatorChar;
-
-
-
- string[] fileList = Directory.GetFileSystemEntries(aimPath);
-
- foreach(string file in fileList)
- {
-
- if(Directory.Exists(file))
- {
- DeleteDir(aimPath+Path.GetFileName(file));
- }
-
- else
- {
- File.Delete (aimPath+Path.GetFileName(file));
- }
- }
-
- System.IO .Directory .Delete (aimPath,true);
- }
- catch (Exception e)
- {
- MessageBox.Show (e.ToString());
- }
- }
-
- 需要引用命名空间:
- using System.IO;
-
- /// <summary>
-
-
-
-
-
-
-
-
-
- public static void CopyFolder(string strFromPath,string strToPath)
- {
-
- if (!Directory.Exists(strFromPath))
- {
- Directory.CreateDirectory(strFromPath);
- }
-
-
- string strFolderName = strFromPath.Substring(strFromPath.LastIndexOf("\\") + 1,strFromPath.Length - strFromPath.LastIndexOf("\\") - 1);
-
-
- if (!Directory.Exists(strToPath + "\\" + strFolderName))
- {
- Directory.CreateDirectory(strToPath + "\\" + strFolderName);
- }
-
- string[] strFiles = Directory.GetFiles(strFromPath);
-
-
- for(int i = 0;i < strFiles.Length;i++)
- {
-
- string strFileName = strFiles.Substring(strFiles.LastIndexOf("\\") + 1,strFiles.Length - strFiles.LastIndexOf("\\") - 1);
-
- File.Copy(strFiles,strToPath + "\\" + strFolderName + "\\" + strFileName,true);
- }
-
-
- DirectoryInfo dirInfo = new DirectoryInfo(strFromPath);
-
- DirectoryInfo[] ZiPath = dirInfo.GetDirectories();
- for (int j = 0;j < ZiPath.Length;j++)
- {
-
- string strZiPath = strFromPath + "\\" + ZiPath[j].ToString();
-
- CopyFolder(strZiPath,strToPath + "\\" + strFolderName);
- }
- }
关闭
站长推荐
/3
文章评论(0条评论)
登录后参与讨论