ASP.NET调用DOS命令

来源:互联网 发布:淘宝网玩具乐高 编辑:程序博客网 时间:2024/05/07 00:10
在用过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"); 
 
原创粉丝点击