如何从C#执行命令行程序并返回STD OUT结果.具体来说,我想对以编程方式选择的两个文件执行DIFF,并将结果写入文本框.是的,我可以为自己解决这个问题,但肯定有人做过类似的事情,我很懒...
//
// Summary:
// Gets or sets a value indicating whether to use the operating system shell
// to start the process.
//
// Returns:
// true to use the shell when starting the process; otherwise, the process is
// created directly from the executable file. The default is true.
[DefaultValue(true)]
[MonitoringDescription("ProcessUseShellExecute")]
[NotifyParentProperty(true)]
public bool UseShellExecute { get; set; }
Run Code Online (Sandbox Code Playgroud)
如果我们生成一个新进程,我们什么时候需要将UseShellExecute设置为True?
我知道如何在.bat脚本中在Windows中创建符号链接:
mklink /J <LinkPath> <OriginalResourcePath>
Run Code Online (Sandbox Code Playgroud)
如何在C#中做同样的事情?
我对谷歌搜索不满意,因为我是C#的初学者,我可能不会使用正确的术语.有人可以指出要使用的API吗?
在C#中,我可以开始一个进程
的Process.Start(的Program.exe);
如何判断程序是否仍在运行,或者程序是否已关闭?
我需要用一些参数启动命令窗口并在里面运行更多命令.
例如,启动test.cmd并运行mkdir.
我可以使用processstartinfo启动test.cmd,但我不确定如何运行更多命令.我可以将更多参数传递给test.cmd进程吗?
我该怎么做?
无法添加评论来回答...所以写在这里.
安德里亚,这就是我想要的.但是上面的代码对我来说不起作用.
我正在启动一个test.cmd,这是一个新的命令环境(比如razzle构建环境),我需要运行更多的命令.
psi.FileName = @"c:\test.cmd";
psi.Arguments = @"arg0 arg1 arg2";
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
Process p = new Process();
p.StartInfo = psi;
p.Start();
p.StandardInput.WriteLine(@"dir>c:\results.txt");
p.StandardInput.WriteLine(@"dir>c:\results2.txt");
Run Code Online (Sandbox Code Playgroud) 我一直试图在C#中获得Windows PC(Windows 7运行.Net 4.5)的总CPU使用率.看起来使用PerformanceCounter应该能够满足我的需求.
我根据以下三个链接编写了一些试用代码(并检查了msdn页面),这是最基本的版本:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace EntropyProject
{
class Program
{
static void Main(string[] args)
{
PerformanceCounter cpuCounter;
cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
while(true)
{
try
{
float firstValue = cpuCounter.NextValue();
System.Threading.Thread.Sleep(500);
Console.WriteLine("Before getting processor:");
float currentCpuUsage = cpuCounter.NextValue();
Console.WriteLine("After getting processor:");
System.Threading.Thread.Sleep(1000);
Console.WriteLine(currentCpuUsage);
}
catch (Exception e)
{
Console.WriteLine("\n{0}\n", e.Message);
}
System.Threading.Thread.Sleep(10000);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
每当调用NextValue时,都会触发下面的异常错误.这似乎是性能计数器值出现问题的常见问题. …
我试图弄清楚如何从在 IIS 7/.Net 4.5 上运行的 C# 运行 bash 命令。
我一直在网上搜索,很多答案都假设您已经安装/就位了某些东西。
我已经Git 1.9.4.msysgit.2安装了 Git Bash 和 Git Giu。我正在寻求一些帮助,了解我还需要安装什么才能运行最简单的 bash 命令。以及如何运行它。
我看过像bash Pipes 这样的帖子 - 我试图从 C# 调用脚本,但它使用cygwin. 如果没有它,我可以做同样的事情吗?如果可以,我该怎么做?
如果我上面的问题没有意义或者似乎提出了单独的问题,那么这就是我的最终目标。我正在尝试编写自己的服务器端 git hook。当开发人员pushes提交到我们的 GitHub 存储库时,我希望 GitHub 调用我们的回调 URL。我希望我的回调 URL 运行一个命令,用刚刚推送的内容git pull更新我们的服务器。staging
我根据我在 GitHub 上提出的上一个问题提出了这个问题- 使用远程服务器设置自动部署。根据那里的答案,我正在尝试运行一个简单的命令,但可以对命令进行硬编码,或者将其放入脚本中并运行它,例如:cd $REPO_DIR && git pull origin $branch_name。
我知道 Jenkins 和其他软件,但我想自己执行这些命令,而不是安装其他软件。
如果需要更多信息,请随时询问。
因此,根据下面的一些答案,我得出了以下结论
using System.Diagnostics;
Process process = new Process();
ProcessStartInfo processStartInfo = …Run Code Online (Sandbox Code Playgroud) 在我的 .NET Core 控制台应用程序中,我收到字符串数组形式的多个命令,并希望将它们作为控制台命令执行(如果可能,但不是硬要求,并在我自己的应用程序中显示它们的输出)。
首先,我尝试解析每个命令以分离它们的名称和参数并将它们放入ProcessStartInfo. 但是,某些命令不起作用(即使是简单的命令,如echo "Hello")。
现在我转而调用 Powershell,如下所示:
static IEnumerable<ProcessStartInfo> ParseCommands(string[] args)
{
return args
.Skip(1)
.Select(q => new ProcessStartInfo()
{
FileName = "powershell",
Arguments = q,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
}).ToList();
}
static void RunCommand(ProcessStartInfo processInfo)
{
Console.WriteLine($"{processInfo.Arguments}");
var process = new Process()
{
StartInfo = processInfo,
};
process.Start();
while (!process.StandardOutput.EndOfStream)
{
Console.WriteLine(process.StandardOutput.ReadLine());
}
process.WaitForExit();
}
Run Code Online (Sandbox Code Playgroud)
问题是我不认为这个可以在 Linux 或 MacOS 上运行。是否有任何“标准”方法来告诉我的应用程序“像控制台命令一样运行它”?
这是我当前的代码通过使用Platform来确定控制台命令,请随时告诉我是否有更好的方法:
static IEnumerable<ProcessStartInfo> ParseCommands(string[] args)
{
var argsPrepend …Run Code Online (Sandbox Code Playgroud)