以下函数是用来在c#中调用“建目录tese1988”DOS命令,编译通过,但只弹出DOS黑框,却并没有建目录,问题出在什么地方呢? public static void ExeCommand()
{
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 = false;//true表示不显示黑框,false表示显示dos界面
string strOutput = null;
try
{
p.Start();
p.StandardInput.WriteLine("mkdir test1988");
p.StandardInput.WriteLine("exit");
p.Close();
}
catch (Exception)
{ } }
{
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 = false;//true表示不显示黑框,false表示显示dos界面
string strOutput = null;
try
{
p.Start();
p.StandardInput.WriteLine("mkdir test1988");
p.StandardInput.WriteLine("exit");
p.Close();
}
catch (Exception)
{ } }
解决方案 »
- 关于C#异常的问题
- c# 关于remoting通信结果判断,在线等
- 跪求正则表达式,提取出其中的公司名!急!!!
- 我用无线路由器接在了adsl猫上,C#如何获得adsl猫分配给无线路由器的IP(不是外网动态IP)
- 问个C#和C++之间数据转换的问题
- 200分求【ACCP5.0】-【.Net/OOP】资源(S2学期的)
- 在程序中设置label的position怎么不行呢?
- 关于ADSI建虚拟目录的一些疑问,高手请进,高分想送您!只写Up的同志不要参与!
- 谁有 CH372的开发资料的例子
- winform程序 treeview,初学。不知道如何绑定数据库。如何点击节点在datagrid中检索相应数据!
- C# 读XML问题扩展
- 高分求c# 使用GET如何模拟浏览网页
{
if (!Directory.Exists("test1988"))
{
Directory.CreateDirectory("test1988");
}
}
catch { }不好吗?
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;namespace testcon
{
class Program
{
static void Main(string[] args)
{
ExeCommand();
} public static void ExeCommand()
{
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 = false;//true表示不显示黑框,false表示显示dos界面
string strOutput = null;
try
{
p.Start();
p.StandardInput.WriteLine("mkdir test1988");
p.StandardInput.WriteLine("exit"); p.Close();
}
catch (Exception)
{ } } }
}
p.StandardInput.WriteLine("exit");这样新目录 c:\test1988
Directory.Delete(路径);
Directory.Move(源路径,目标路径);
System.Diagnostics.Process.Start("cmd", "/c" + command);方法二:把DOS命令写成一个bat批处理,直接用Process.Start运行批处理文件
正像8L说的,只是为了练习C#中运行DOS命令,并非真的为了建目录。我已试过了,楼上都是正解。现在有个问题,我真实的想法是想调用批处理,确实批处理也调用成功了,但我现在想把调用批处理的过程中的提示显示在控制台的黑屏中,让用户知道确实批处理调用了,目前的状况是控制台的黑屏中没有任何提示。我修改了程序如下: public static string ExeCommand(string commandText)
{
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 = false;//true表示不显示黑框,false表示显示dos界面
string strOutput = null;
try
{
p.Start();
p.StandardInput.WriteLine(commandText);
//p.StandardInput.WriteLine("exit");
strOutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
}
catch (Exception e)
{
strOutput = e.Message;
}
return strOutput;
}
private void 系统大扫除ToolStripMenuItem_Click(object sender, EventArgs e)
{
string dosCommand = "系统大扫除.bat";
string strRst = ExeCommand(dosCommand);
Console.WriteLine(strRst);
Console.ReadLine();
}
但控制台的黑屏中仍然没有任何提示。
C:\Documents and Settings\Administrator>cmd/?
启动 Windows XP 命令解释程序一个新的实例CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:
[[/S] [/C | /K] string]/C 执行字符串指定的命令然后终断
/K 执行字符串指定的命令但保留
/S 在 /C 或 /K 后修改字符串处理(见下)
/Q 关闭回应
/D 从注册表中停用执行 AutoRun 命令(见下)
/A 使向内部管道或文件命令的输出成为 ANSI
/U 使向内部管道或文件命令的输出成为 Unicode
/T:fg 设置前景/背景颜色(详细信息,请见 COLOR /?)
/E:ON 启用命令扩展(见下)
/E:OFF 停用命令扩展(见下)
/F:ON 启用文件和目录名称完成字符 (见下)
/F:OFF 停用文件和目录名称完成字符(见下)
/V:ON 将 ! 作为定界符启动延缓环境变量扩展。如: /V:ON 会
允许 !var! 在执行时允许 !var! 扩展变量 var。var 语法
在输入时扩展变量,这与在一个 FOR 循环内不同。
/V:OFF 停用延缓的环境扩展。
能显示,至少结合WINAPI可以实现,你自己试吧。我对这种实现方式实在是不感兴趣了。能用windows实现的,就尽量避开用DOS。从DOS --> 控制台 --> WINFORM --> WPF 技术发展那么快。人的时间是有限的,要学习最有用的知识。
说明:经常有朋友问如何在C#中运行一个dos命令,并截取输出、输出流的问题,这个问题我以前在Java中实现过,由于在C#中没有遇到过类似的情况,为了避免每次别人问都要一遍一遍演示的情况,特地做了一个简单的例子,实现在WinForm中ping一个网站,并且将ping的结果显示在一个文本框中。有图有真相: private void btnExecute_Click(object sender, EventArgs e)
{
tbResult.Text = "";
ProcessStartInfo start = new ProcessStartInfo("Ping.exe");//设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到
//如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe
start.Arguments = txtCommand.Text;//设置命令参数
start.CreateNoWindow = true;//不显示dos命令行窗口
start.RedirectStandardOutput = true;//
start.RedirectStandardInput = true;//
start.UseShellExecute = false;//是否指定操作系统外壳进程启动程序
Process p=Process.Start(start);
StreamReader reader = p.StandardOutput;//截取输出流
string line = reader.ReadLine();//每次读取一行
while (!reader.EndOfStream)
{
tbResult.AppendText(line+" ");
line = reader.ReadLine();
}
p.WaitForExit();//等待程序执行完退出进程
p.Close();//关闭进程
reader.Close();//关闭流
}
txtCommand是什么啊?错误 1 当前上下文中不存在名称“txtCommand” E:\c#\gold\gold3\ClearSystem.cs 29 31 gold
如果想在DOS窗口显示输入输出,试试
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);// 查找DOS窗口
IntPtr hwnd= FindWindow ("ConsoleWindowClass", null); Sendkey.send(...)