c# 调用CMD命令执行dos命令

来源:互联网 发布:句子迷 知乎 编辑:程序博客网 时间:2024/04/25 15:02
 

public bool ExecuteCmd(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.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

 

调用:string str_attrib = string.Format("attrib -R -H -A -S \"{0}\\*.*\" /S /D",folder);

            ExecuteCmd(str_attrib);                         //去除文件夹及其子目录的隐藏,只读属性等

            ExecuteCmd(@"md d:\abc\mydir");     //建目录  

原创粉丝点击