我用C#调用dos,想输入多行命令的方法。但是往往只有第一行命令被执行。
触发命令如下:
private void button3_Click(object sender, EventArgs e)
{
string m;
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start(); p.StandardInput.WriteLine("del /f /s /q %systemdrive%\\*.tmp\rdel /f /s /q %systemdrive%\\*._mp\rdel /f /s /q %systemdrive%\\*.log\rdel /f /s /q %systemdrive%\\*.gid\rdel /f /s /q %systemdrive%\\*.chk\rdel /f /s /q %systemdrive%\\*.old\rdel /f /s /q %systemdrive%\\recycled\\*.*\rdel /f /s /q %windir%\\*.bak\rdel /f /s /q %windir%\\prefetch\\*.*\rrd /s /q %windir%\\temp & md %windir%\\temp\rdel /f /q %userprofile%\\recent\\*.*\rdel /f /s /q \"%userprofile%\\Local Settings\\Temporary Internet Files\\*.*\"\rdel /f /s /q \"%userprofile%\\Local Settings\\Temp\\*.*\"\rdel /f /s /q \"%userprofile%\\recent\\*.*\""); p.StandardInput.WriteLine("exit");
}
我也试过把要输入的命令赋值给n,但是一样不行。特来求救!!!HELP!!
触发命令如下:
private void button3_Click(object sender, EventArgs e)
{
string m;
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start(); p.StandardInput.WriteLine("del /f /s /q %systemdrive%\\*.tmp\rdel /f /s /q %systemdrive%\\*._mp\rdel /f /s /q %systemdrive%\\*.log\rdel /f /s /q %systemdrive%\\*.gid\rdel /f /s /q %systemdrive%\\*.chk\rdel /f /s /q %systemdrive%\\*.old\rdel /f /s /q %systemdrive%\\recycled\\*.*\rdel /f /s /q %windir%\\*.bak\rdel /f /s /q %windir%\\prefetch\\*.*\rrd /s /q %windir%\\temp & md %windir%\\temp\rdel /f /q %userprofile%\\recent\\*.*\rdel /f /s /q \"%userprofile%\\Local Settings\\Temporary Internet Files\\*.*\"\rdel /f /s /q \"%userprofile%\\Local Settings\\Temp\\*.*\"\rdel /f /s /q \"%userprofile%\\recent\\*.*\""); p.StandardInput.WriteLine("exit");
}
我也试过把要输入的命令赋值给n,但是一样不行。特来求救!!!HELP!!
解决方案 »
- 利用短信远程关闭计算机
- 64位Win7下C#开发基于Tibco的系统的问题
- C# 程序崩溃问题
- winform 中 datagrid 数据填充
- 关于 dataGridview 的数据源问题!!!在线等!
- 异步调用问题
- 谁用过volatile,能讲讲有什么好处?
- 请推荐几本你认为最好的C#及WebService编程的书籍.(从入门到精通)
- 请问谁看过wrox的《c#数据库入门经典》这本书?怎么样啊?
- xdev(Gallant)来拿分!!急SOS!SOS!SOS!!希望2天内能有人回答!!有谁知道如何用c#分割文件??如果解决了300分相送!!!
- C#winform编写好的程序
- 知道客户端文件路径,如何上传文件?
后来,我直接把批处理写在一个*.bat文件中,然后在用c#调用这个批处理,这样还省事。
我想只建立一个。exe文件。
或者...如果LZ熟悉匿名管道的话可以试试重定向cmd的IO,
在Input的一端写入命令让cmd去执行.
string backupcmd = string.Format(" exp {0}/{1}@{2} owner={0} file={3} compress=y", user, pwd, sid, outputfile);System.Diagnostics.Process backupprocess;
backupprocess = System.Diagnostics.Process.Start("cmd.exe", "/c " + backupcmd);
backupprocess.WaitForExit(); // 等待处部进程处理结束,同步方式
int exitcode = backupprocess.ExitCode;
backupprocess.Close();return exitcode == 0;