从从vscode启动的程序中获取数据

时间:2019-04-17 08:48:16

标签: c# visual-studio-code

我有一个vscode扩展名,可以启动一个可执行文件,我知道如何将数据从vscode传递到我的程序中,但是反之亦然。

// class that launches the exe
class Execute {
  constructor(private _extensionPath: string) {}

  public Launch() {
    console.log('213');
    Process.exec(
      `WpfApp1.exe true`,
      { cwd: this._extensionPath },
      (error: Error, stdout: string, stderr: string) => {
        if (stdout.length === 0) {
          return;
        }
      }
    );
  }
}


// calling the class
let exe = new Execute(
vscode.extensions.getExtension('author.extension').extensionPath
);
exe.Launch();

c#接收数据

void App_Startup(object sender, StartupEventArgs e)
{
    try
    {
        test_p = e.Args[0];
        if (test_p == "true")
        {

        }
        }
        catch { MessageBox.Show("fail"); }
}

如何将数据从C#应用程序发送到vscode扩展? 在vscode中调用函数会更好。

2 个答案:

答案 0 :(得分:1)

您还可以使用c#运行可执行文件:

public static string[] Cmd(bool xWaitForExecution, params string[] xCommands)
{
    //PROCESS CMD
    if (xCommands == null || xCommands.Length == 0) return null;
    ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
    info.CreateNoWindow = true;
    info.UseShellExecute = false;
    info.RedirectStandardInput = true;          //STD INPUT
    info.RedirectStandardOutput = true;         //STD OUTPUT
    info.RedirectStandardError = true;          //STD ERROR
    Process process = Process.Start(info);

    //WRITE COMMANDS
    using (StreamWriter sw = process.StandardInput)
        if (sw.BaseStream.CanWrite)
            foreach (string cmd in xCommands)
                sw.WriteLine(cmd);

    //GET OUTPUT & ERROR
    if (!xWaitForExecution) return null;
    string output = process.StandardOutput.ReadToEnd();     //OUTPUT
    string error = process.StandardError.ReadToEnd();       //ERROR
    string exit = process.ExitCode.ToString();              //EXIT CODE
    process.Close();
    return new string[] { output, error, exit };
}

该函数运行cmd64.exe,并且应使用类似的方式

//Call Cmd, true means the c# application will wait for the complete execute of your executable (needed to obtain output values)
string[] ret = Cmd(true, "\\mypath\\my.exe -Argument1 -Argument2"); //Passing arguments depends on your executable
string output = ret[0];
Console.WriteLine(ret[0]) //printed arguments from your executable (for instance python: print("Argument1"))

目前还不清楚为什么需要VS Code扩展来执行可执行文件。这是从c#在Windows上运行可执行文件的一种可行选择。

答案 1 :(得分:0)

发送数据:

 private void AnyEvent(object sender, MouseEventArgs e)
    {
      const String output = "testOutput,testOutput2";
      Console.Write(output);
    }

接收数据:

 import * as Process from 'child_process';
 // put the code below in a class or function.
 Process.exec(
  `app.exe input`,
  { cwd: "Path to folder of the executable" },
  (error, stdout: string, stderr: string) => {
    const output = stdout.split(','); //it only outputs data after the exe is closed!?
    if (output[0] === 'testOutput') {
      console.log('Output: == "testOutput"');
    }
  }
);

如果未在下面打开问题或评论,则示例Project在运行npm i之后应该可以工作。