简体   繁体   English

从 C# 运行 Curl 命令

[英]Running a Curl Command from C#

I'm trying to run a curl command from a C# program.我正在尝试从 C# 程序运行 curl 命令。 My code is below.我的代码如下。 When I run the code below, I get an exception that the file is not found.当我运行下面的代码时,我得到一个找不到文件的异常。 I want to be able to do this but I do not want to use a batch file as a parameter for the filename.我希望能够做到这一点,但我不想使用批处理文件作为文件名的参数。 That is because the arguments for my curl command are variable based upon other conditions in the C# code.这是因为我的 curl 命令的 arguments 根据 C# 代码中的其他条件而变化。 My variable strCmdText has the arguments for the curl command (the source and destination files).我的变量 strCmdText 具有用于 curl 命令(源文件和目标文件)的 arguments。 There are other examples of this on Stackoverflow, but they all use a batch file which I'm trying to avoid. Stackoverflow 上还有其他示例,但它们都使用我试图避免的批处理文件。

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "C:\\Windows\\System32\\curl.exe";
p.StartInfo.Arguments = strCmdText;
p.StartInfo.UseShellExecute = false;
p.Start();
p.WaitForExit();

I changed my code to the following:我将代码更改为以下内容:

System.Diagnostics.ProcessStartInfo p = new 
    System.Diagnostics.ProcessStartInfo();
    p.UseShellExecute = true;
    p.WorkingDirectory = "C:\\Windows\\System32\\";
    p.FileName = "curl.exe";
    p.ErrorDialog = true;
    p.CreateNoWindow = true;
    System.Diagnostics.Process.Start(p);

From a DOS prompt, curl does exist in this directory.在 DOS 提示符下,curl 确实存在于该目录中。 But I still get the curl not found message.但我仍然收到 curl 未找到消息。 Something has to be strange with the path here.这里的路径一定很奇怪。 When I put a break point in though, and view the Environment class, System32 is in the path.但是,当我在其中设置断点并查看环境 class 时,System32 在路径中。

Curl is available at the location: C:\Windows\System32\curl.exe Curl 在以下位置可用: C:\Windows\System32\curl.exe

That only leaves the source file to be the culprit of a "File not found" issue.这只会使源文件成为“找不到文件”问题的罪魁祸首。

As you're launching curl through a process, ensure that your paths are escaped properly in your startup arguments.当您通过进程启动 curl 时,请确保您的路径在启动 arguments 中正确转义。

Alternatively, you could launch curl through cmd (through a process), you can try with the following, changing the command-line arguments from --help to suit your desired action.或者,您可以通过 cmd 启动 curl(通过进程),您可以尝试以下操作,将命令行 arguments 从--help更改为适合您所需的操作。

string script = $"\"C:\\Windows\\System32\\curl.exe\" --help";

Process process = new Process()
{
    StartInfo = new ProcessStartInfo()
    {
        FileName = "cmd",
        Arguments = script
    }
};

process.Start();

Please note that this is in principle, essentially using a batch file as it's just throwing some commands into a cmd.请注意,这原则上是使用批处理文件,因为它只是将一些命令放入 cmd 中。

I had the exact same problem.我有同样的问题。 Just delete curl.exe from System32 and place it on another folder (dont't forget the dependences, dlls, etc.).只需从 System32 中删除 curl.exe 并将其放在另一个文件夹中(不要忘记依赖项、dll 等)。

Then in the line然后在行

p.StartInfo.FileName = "C:\\Windows\\System32\\curl.exe";

Overwrite "C:\\Windows\\System32\\curl.exe" to "C:\\NEW PATH\\curl.exe" ."C:\\Windows\\System32\\curl.exe"覆盖为"C:\\NEW PATH\\curl.exe"

Note: You MUST delete it from System32.注意:您必须从 System32 中删除它。 If you just copy to the new location it will still don't work.如果您只是复制到新位置,它仍然无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM