当前位置:编程学习 > C#/ASP.NET >>

使用ASP.NET程序来管理文件或目录的一种方案

答案:     使用ASP.net程序来管理文件或目录有多种方法,比如Directory.CreateDirectory,DirectoryInfo.Create等方法来创建一个目录,但今天我们来介绍另外一种比较另类的创建目录的方法,就是使用cmd的命令方式来管理。
  
  
  在用过DOS命令的人都知道DOS命令可以做很多事,比如dir可以列举目录下的文件和子目录。这次,我们就是用ASP.NET程序来调用cmd的命令来管理文件。
  下面是一段调用cmd.exe的方法:
  public bool cmd(string argm)
  {
  //开始创建文件
  Process p = new Process();
  p.StartInfo.FileName = "cmd.exe";
  p.StartInfo.UseShellExecute = false;
  p.StartInfo.RedirectStandardInput = true;
  p.StartInfo.RedirectStandardOutput = true;
  p.StartInfo.RedirectStandardError = true;
  p.StartInfo.CreateNoWindow = true;
  
  
  try
  {
  p.Start();
  p.StandardInput.WriteLine(argm);
  p.StandardInput.WriteLine("exit");
  p.StandardOutput.ReadToEnd();
  p.Close();
  return true;
  }
  catch
  {
  return false;
  }
  }
  其中argm是表示执行的cmd命令,比如我要创建一个文件夹,使用方法如下:
  bool created = cmd(@"md e:\abc\mydir");
  
  
  使用cmd进行文件管理的命令有如下: 创建文件夹 md
  删除文件夹 rd
  删除文件 del
  重命名文件夹 move
  重命名文件 rename
  复制文件夹 xcopy
  复制文件 copy
  移动文件或文件夹 move
  
  
  
  

上一个:ASP.NET 2.0中的DataSource系列控件(1)
下一个:用ASP.NET结合XML制作广告管理程序(2)

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,