Tag: cmd

C#:重定向已经运行的进程的标准输出

我一直很难获得“子流程”的输出(一个由我通过c#System.Diagnostics.Process监控的黑盒流程内部启动) 我接受了上一篇文章的回答: 这里 。 在那里你可以找到我经历过的细节。 此时,虽然我能够找到由process1.exe生成的ssh进程,但我正在监视。 我无法将输出重定向到我的c#程序,因为它是一个“已在运行的进程”,并且不是直接从C#启动的。 看来,您在System.Diagnostics.Process对象上设置的所有属性只有在从c#应用程序显式启动该进程时才会生效; 如果某个其他“非托管进程”已启动该进程,则设置此重定向无效,因为该进程已由未指定我需要的重定向的内容启动。 有没有办法重定向已经启动的进程的输出(由程序启动的进程,我没有在启动此进程之前预先指定重定向的范围)?

是否可以在C#/ .Net中将消息记录到cmd.exe?

是否可以将消息从WinForms应用程序记录到cmd.exe进程以编程方式启动? 我尝试过以下代码的各种变体: private void button1_Click(object sender, EventArgs e) { Log(“Button has been pressed”); } private void Log(string message) { var process = Process.Start(new ProcessStartInfo(“cmd.exe”, @”/K “”more”””) { UseShellExecute = false, RedirectStandardInput = true, }); process.StandardInput.WriteLine(message); } 不幸的是,控制台窗口闪烁半秒钟就是这样。 请不要回答我真正想做的事情,因为现在我只是好奇,如果可能的话:)

无法将命令发送到cmd.exe进程

我正在尝试使用StandardInput.WriteLine(str)将命令发送到打开的cmd.exe进程,但是似乎没有发送任何命令。 首先,我使用全局变量p( Process p )打开一个过程。 p = new Process() { StartInfo = { CreateNoWindow = true, UseShellExecute = false, RedirectStandardError = true, RedirectStandardInput = true, RedirectStandardOutput = true, FileName = @”cmd.exe”, Arguments = “/C” //blank arguments } }; p.Start(); p.WaitForExit(); 之后,我尝试使用一种简单的方法发送命令,该方法将结果记录在文本框中。 private void runcmd(string command) { p.StandardInput.WriteLine(command); var output = p.StandardOutput.ReadToEnd(); TextBox1.Text = output; } […]

如何在C#中运行静默安装程序

我有以下C#代码: string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); Process.Start(“cmd.exe”, “/c” + desktopPath + “\\” + “MyInstaller_7.1.51.14.exe –s –v –qn”); 第一行获取.exe所在桌面的路径。 字符串desktopPath用于第二行。 第二行应该以静默方式启动安装程序,以便进程在后台运行,安装向导根本不显示。 在命令提示符下运行desktopPath + “\\” + “MyInstaller_7.1.51.14.exe –s –v –qn”的字符串结果工作正常,安装程序以静默方式运行。 万一有人想知道,字符串的结果 desktopPath + “\\” + “MyInstaller_7.1.51.14.exe –s –v –qn” 是 C:\Users\ME\Desktop\MyInstaller_7.1.51.14.exe -s -v -qn 并在命令提示符下运行此命令以静默方式运行安装。 不幸的是,在C#代码中触发相同的命令: Process.Start(“cmd.exe”, “/c” + desktopPath + “\\” + “MyInstaller_7.1.51.14.exe –s –v –qn”); 不以静默方式运行安装程序。 […]

从cmd运行时控制台和winforms应用程序之间的区别

我有一个winforms应用程序,有时从命令行使用。 这是代码(当然简化): [STAThread] static void Main() { AttachConsole(ATTACH_PARENT_PROCESS); Console.WriteLine(“Hello”); /*Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1());*/ } 如果那是一个控制台应用程序,输出可能是: C:\ConsoleApplication\ConsoleApplication.exe Hello C:\ConsoleApplication\_ 在Windows应用程序的情况下,它实际上: C:\WindowsApplication\WindowsApplication.exe C:\WindowsApplication\Hello _ 任何人都可以告诉我为什么我们有这样的差异,是否有可能使我的Windows应用程序在从cmd运行时表现得像控制台? 编辑: 我希望我的Windows应用程序在从cmd运行时表现得像控制台: C:\WindowsApplication\WindowsApplication.exe Hello C:\WindowsApplication\_ 解: 结果我正在运行我的应用程序 C:\WindowsApplication\start /wait WindowsApplication.exe

使用C#中的参数以管理员身份运行CMD

我希望以C#中的参数作为管理员运行cmd.exe ,以防止出现UAC弹出窗口。 这是将其用作自动安装过程所必需的。 我传入的命令只是安装文件(.exe)的路径,其中/q用于安静安装。 当我运行此代码时,有一个CMD弹出窗口,但它运行就好像它没有执行任何操作。 public static string ExecuteCommandAsAdmin(string command) { ProcessStartInfo procStartInfo = new ProcessStartInfo() { RedirectStandardError = true, RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true, FileName = “runas.exe”, Arguments = “/user:Administrator cmd /K ” + command }; using (Process proc = new Process()) { proc.StartInfo = procStartInfo; proc.Start(); string output = […]

Foo.cmd不会输出正在处理的行(在网站上)

我在理解.NET中ProcessStartInfo类的in和out时遇到了问题。 我使用这个类来执行像FFmpeg这样的.exe程序,没有任何问题。 但是,当我使用ProcessStartInfo启动.cmd程序时,就像只包含@echo Hello world的简单foo.cmd一样,它不会输出任何东西。 ProcessStartInfo oInfo = new ProcessStartInfo(@”C:\Program Files (x86)\itms\foo.cmd”) { UseShellExecute = false, RedirectStandardError = true, RedirectStandardOutput = true, CreateNoWindow = true }; using (Process p = new Process()) { p.StartInfo = oInfo; p.OutputDataReceived += new DataReceivedEventHandler(transporter_OutputDataReceived); p.Start(); p.BeginOutputReadLine(); p.WaitForExit(); } private void transporter_OutputDataReceived(object sender, DataReceivedEventArgs e) { Response.Write(e.Data + ” – […]

在C#中向cmd提示符发送命令

对于我的一个实现,我正在研究一个应该向cmd窗口发送/检索命令/结果的工具。 一切正常,但下面的用例无法做任何事情。 似乎我的应用程序正在等待某事(而不是显示结果) 从我的工具我导航到python文件夹。 从python文件夹我尝试启动python.exe但此时,我的编辑器没有做任何事情。 它只是在等待。 为了您的善意考虑,我也在这里链接video。 你们会更容易理解我想说的话。 在此处观看video(在youtube上) 我还附上了我目前拥有的代码。 ProcessStartInfo info = new ProcessStartInfo(“cmd.exe”); string argument = null; if (!string.IsNullOrEmpty(startingDirectory) && System.IO.Directory.Exists(startingDirectory)) { argument += @”cd\”; } else { argument += “\””; } info.Arguments = argument; info.CreateNoWindow = true; info.RedirectStandardError = true; info.RedirectStandardInput = true; info.RedirectStandardOutput = true; info.UseShellExecute = false; this.shellProcess = System.Diagnostics.Process.Start(info); […]

如何将一系列命令发送到命令窗口进程?

我们的网络路径上有一些命令(批处理文件/可执行文件),我们必须调用它来初始化该命令窗口的“开发环境”。 它设置了一些环境变量,向Path添加了东西等等。(然后,只有我们输入的任何工作命令都会被识别,我不知道那些初始化命令内部的内容) 现在我的问题是,我想使用C#程序调用一系列“工作命令”,当然,只有初始设置完成后它们才能工作。 我怎样才能做到这一点? 目前,我正在从程序中创建一个批处理文件,例如: file.Writeline(“InitializationStep1.bat”) file.Writeline(“InitializeStep2.exe”) file.Writeline(“InitializeStep3.exe”) 然后是实际的命令 file.Writeline(“Dowork -arguments -flags -blah -blah”) file.Writeline(“DoMoreWork -arguments -flags -blah -blah”) 然后最后关闭文件编写器,并运行此批处理文件。 现在如果我使用Process.RunStart(“cmd.exe”,”Dowork -arguments”);直接执行它Process.RunStart(“cmd.exe”,”Dowork -arguments”); 它不会运行。 如何以更干净的方式实现这一点,以便我只需要运行一次初始化命令? (我每次使用所有三个初始化程序都可以运行cmd.exe ,但是它们需要花费很多时间,所以我只想做一次)

通过命令行构建Visual Studio项目

我从Windows Server 2008安装运行ASP.NET网站,我喜欢通过命令行编辑页面,因为我进入服务器。 我在服务器上安装了Vim ,以便我可以轻松编辑文件。 如果我编辑HTML和CSS以及.aspx页面,则更新成功。 但如果我想编辑源代码,我将不得不重建项目。 重建项目可以很好地重新编译所有内容并更新Web上的副本。 这是一个开发服务器,所以更新到一切都很好,因为没有人看到这个服务器。 如何通过命令行构建项目以更新源代码并在服务器上构建? 该项目是用C#编写的,文件都在wwwroot文件夹中,因此在构建之后不需要进行文件移动。