温柔网

 找回密码
 注册

查看: 587|回复: 0

使用ASP.NET程序来管理文件或目录有多种方法

[复制链接]
发表于 2004-11-30 20:31:59 | 显示全部楼层 |阅读模式
比如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
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|手机版|小黑屋|温柔网 ( 浙ICP备13033583号-8 )

GMT+8, 2024-5-21 02:06 , Processed in 0.035198 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表