如何从控制台应用程序运行程序?
我正在寻找包含创建控制台应用程序示例的资源。我已经过了“Hello World”阶段,但在需要运行应用程序的时候遇到了困难。我有需要运行的字符串,该字符串是从我试图在 C# 应用程序中自动化的批处理文件中提取的。我需要帮助了解哪些类和命名空间具有运行它所需的功能。
编辑:很抱歉问得不好。我会重写它。
我正在尝试创建一个控制台应用程序来替换我部分编写的批处理文件。我需要做的一些数据和文件操作比在批处理文件中轻松完成的更复杂。到目前为止,我可以很好地读取、写入和操作这些文件。我在尝试弄清楚如何运行命令来在服务器上执行应用程序并传入正确的参数时遇到困难。
更新:一位同事给了我以下代码片段,这正是我继续前进所需要的。我很抱歉这个问题的措辞如此糟糕。
public static string MyDOSMethod()
{
ProcessStartInfo si = new ProcessStartInfo("cmd.exe");
si.RedirectStandardInput = true;
si.RedirectStandardOutput = true;
si.UseShellExecute = false;
Process p = Process.Start(si);
p.StandardInput.WriteLine(@"cd \windows\system32");
p.StandardInput.WriteLine("exit");
try
{
return p.StandardOutput.ReadToEnd();
}
catch (Exception e)
{
return e.Message;
}
}
I am looking for resources that have examples for creating a Console Application. I am past the "Hello World" stage but am stumped at the point where I need to run an application. I have the string that I need to run that I pulled from the batch file that I am trying to automate in a C# app. I need help with knowing which classes and namespaces have the functionality I need to run it.
Edit: Sorry for the poorly asked question. I'll rewrite it.
I am trying to create a console application that will replace a batch file that I have partially written. Some of the data and file manipulations that I need to do are more complex than can easily be done in a batch file. So far I am reading, writing, and manipulating the files just fine. I am having difficulty when trying figure out how to run a command to execute an application on the server with the proper arguments being passed in.
Update: a coworker gave me the following code snippit which is exactly what I needed to move forward. I am sorry the question was worded so badly.
public static string MyDOSMethod()
{
ProcessStartInfo si = new ProcessStartInfo("cmd.exe");
si.RedirectStandardInput = true;
si.RedirectStandardOutput = true;
si.UseShellExecute = false;
Process p = Process.Start(si);
p.StandardInput.WriteLine(@"cd \windows\system32");
p.StandardInput.WriteLine("exit");
try
{
return p.StandardOutput.ReadToEnd();
}
catch (Exception e)
{
return e.Message;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题并不完全清楚,我的理解是“我需要从控制台应用程序启动一个应用程序;我可以使用什么类?”
我的答案是:您应该看看静态方法
Process.Start
(通常在类Process
命名空间System.Diagnostics
)。The question is not perfectly clear, what I understood is "I need to start an application from my console application; what class can I use?"
My answer is: you should have a look at the static method
Process.Start
(and in general at the classProcess
of namespaceSystem.Diagnostics
).看看本教程,它将指导您完成
Process.Start 和
ProcessStartInfo
(运行进程并为您提供反馈)Have a look at this tutorial, it will guide you through the usage of
Process.Start
andProcessStartInfo
(which runs a process and gives you feedback)如果您是编程新手,我推荐您阅读这本 C# 简介。
http://www.csharp-station.com/Tutorial.aspx
以便编译你需要一个编译器和一个 GUI 来编辑代码也很好 :o) 在这里你可以使用 Visual Studio 的免费版本:
http://www.microsoft.com/express/
在 Visual Studio 中,只需单击新的选择控制台应用程序,我想您将获得一个“默认应用程序”,例如您可以运行和构建的 Hello World。
I recommend this introduction to C# if your new to programming.
http://www.csharp-station.com/Tutorial.aspx
In order to compile you need a compiler and a GUI to edit code in is also nice :o) Here you can use the free version of Visual Studio:
http://www.microsoft.com/express/
In Visual Studio just click new select console application and i think you will get a "default application" like Hello World that you can run and build.