我能理解cmd但不能理解cmd /c.我试图从我使用的当前调用java程序Runtime.getRuntime().exec("cmd /C java helloworld");出现我怀疑.
我有以下代码:
info = new System.Diagnostics.ProcessStartInfo("TheProgram.exe", String.Join(" ", args));
info.CreateNoWindow = true;
info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
System.Diagnostics.Process p = System.Diagnostics.Process.Start(info);
p.WaitForExit();
Console.WriteLine(p.StandardOutput.ReadToEnd()); //need the StandardOutput contents
Run Code Online (Sandbox Code Playgroud)
我知道我开始的进程的输出大约是7MB.在Windows控制台中运行它可以正常工作.不幸的是,这会在WaitForExit上无限期挂起.另请注意,对于较小的输出(例如3KB),此代码不会挂起.
ProcessStartInfo中的内部StandardOutput是否可能无法缓冲7MB?如果是这样,我该怎么做呢?如果没有,我做错了什么?
有没有办法执行JavaScript并使用Visual Studio代码显示结果?
例如,包含的脚本文件
console.log('hello world');
Run Code Online (Sandbox Code Playgroud)
我假设将需要nodejs,但无法解决如何做到这一点?
编辑:通过"Visual Studio代码"我的意思是来自Microsoft的新代码编辑器 - 不是使用Visual Studio编写的代码
如何从.NET应用程序调用控制台应用程序并捕获控制台中生成的所有输出?
(请记住,我不想先将信息保存在文件中,然后重新保存,因为我希望将其作为实时信息接收.)
我想从我在Powershell脚本中启动的进程捕获stdout和stderr,并将其异步显示到控制台.我已经通过MSDN和其他博客找到了一些关于这样做的文档.
创建并运行下面的示例后,我似乎无法异步显示任何输出.所有输出仅在进程终止时显示.
$ps = new-object System.Diagnostics.Process
$ps.StartInfo.Filename = "cmd.exe"
$ps.StartInfo.UseShellExecute = $false
$ps.StartInfo.RedirectStandardOutput = $true
$ps.StartInfo.Arguments = "/c echo `"hi`" `& timeout 5"
$action = { Write-Host $EventArgs.Data }
Register-ObjectEvent -InputObject $ps -EventName OutputDataReceived -Action $action | Out-Null
$ps.start() | Out-Null
$ps.BeginOutputReadLine()
$ps.WaitForExit()
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我希望在程序执行结束之前在命令行上看到"hi"的输出,因为应该触发OutputDataReceived事件.
我已经尝试过使用其他可执行文件 - java.exe,git.exe等.所有这些都具有相同的效果,所以我不得不认为有一些我不理解或错过的简单.还需要做什么才能异步读取stdout?
在 .NET Framework 中,要获取操作系统版本,您可以使用和值Environment.OSVersion来告诉您 Windows 版本(即 6.1 = Windows 7、10.0 = Windows 10)。尽管 Windows 11 已经发布(开发版和测试版渠道)一个多月了,但文档尚未更新以提及如何检测 Windows 11。MajorMinor
对于 Windows API,GetVersion已永久弃用,甚至版本帮助程序 API 也只能升至IsWindows10OrGreater. 是否有简单的检查来确定主要的 Windows 版本,特别是 Windows 11?有人有类似的问题,但对于 Delphi(How to detector Windows 11 using Delphi 10.3.3),接受的答案都是 hacks。为什么微软这么难提供一个简单的API来返回当前的系统版本呢? GetVersion永远不应该被弃用。
我想从c#运行一个shell命令并使用我程序中的返回信息.所以我已经知道要从终端运行一些东西我需要做类似的事情
string strCmdText;
strCmdText= "p4.exe jobs -e";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);
Run Code Online (Sandbox Code Playgroud)
所以现在命令执行了,从这个命令返回一些信息...我的问题是如何在我的程序中使用这些信息,可能与命令行参数有关,但不确定...
我知道使用python这样的脚本语言更容易,但是真的需要使用c#
我试图在ASP.Net Web应用程序中的上传文件上运行防病毒扫描.我们正在使用Sophos,因此可以访问他们的命令行API sav32cli.在我使用的代码中:
Process proc = new Process();
proc.StartInfo.FileName = @"C:\Program Files (x86)\Sophos\Sophos Anti-Virus\sav32cli.exe";
proc.StartInfo.Arguments = @"-remove -nc " + SavedFile;
proc.StartInfo.Verb = "runas";
proc.Start();
proc.WaitForExit();
int exitCode = proc.ExitCode;
Run Code Online (Sandbox Code Playgroud)
当单步执行代码时,当连接到w3wp开发服务器上的进程时,代码只是从一行跳到另一行似乎什么都不做.从dev服务器上的代码运行时,它会执行预期的扫描文件并删除它是否被视为病毒.
服务器正在运行IIS 8.0,以及.Net Framework 4中内置的应用程序.我已根据这些说明更改了机器配置以允许进程作为SYSTEM帐户运行.https://support.microsoft.com/en-us/kb/317012#%2Fen-us%2Fkb%2F317012
<processModel userName="SYSTEM" password="AutoGenerate" />
Run Code Online (Sandbox Code Playgroud)
有什么我想念的吗?这种实施的最佳实践是什么?
编辑:调用时,Process返回ExitCode2(错误停止执行),而不是预期的0(扫描工作,没有病毒),或3(扫描工作,发现病毒).
编辑2:根据下面的评论,我将代码更改为:
Process proc = new Process();
proc.StartInfo.FileName = @"C:\Program Files (x86)\Sophos\Sophos Anti-Virus\sav32cli.exe";
proc.StartInfo.Arguments = @"-remove -nc " + SavedFile;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
StringBuilder output = …Run Code Online (Sandbox Code Playgroud) 我想从我的c#app在cmd上运行命令.
我试过了:
string strCmdText = "ipconfig";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
Run Code Online (Sandbox Code Playgroud)
结果:
cmd窗口弹出,但命令没有做任何事情.
为什么?
我正在尝试使用简单的 C# 控制台应用程序获取 Google 授权代码。整个过程非常简单:使用包含Client IdGoogle Oauth2 唯一端点的 url 发送请求,登录 Google 帐户,然后获取授权码:
string url = "https://accounts.google.com/o/oauth2/v2/auth?whatever";
Process.Start("chrome.exe", url); // open up browser so user can log in
// get the auth code from chrome
Run Code Online (Sandbox Code Playgroud)
如何从 Google Chrome 获取生成的代码并将其传递回我的应用程序?
c# ×6
.net ×3
cmd ×2
antivirus ×1
asp.net ×1
asynchronous ×1
command-line ×1
console ×1
debugging ×1
events ×1
google-oauth ×1
iis ×1
node.js ×1
oauth-2.0 ×1
powershell ×1
process ×1
shell ×1
winapi ×1
windows ×1