简体   繁体   English

将批处理文件调用的C#程序的输出返回到该批处理文件,而不使用临时文件的读/写操作

[英]returning the output of a C# program called by a batch file to that batch file without using temp file read/writes

what I'm doing 我在做什么

This first section describes why I believe this question is not a duplicate. 第一部分描述了为什么我认为这个问题不是重复的。 I'm trying to get some output of a C# windows forms application into a variable in batch. 我试图将C#Windows窗体应用程序的某些输出批量添加到变量中。 There are some caveats however. 但是有一些警告。 Due to the requirements of this project (it is for work), I can't make temp files to simply write to from C# and read from batch. 由于该项目的要求(用于工作),我无法制作临时文件来简单地从C#写入并从批处理中读取。 This is because it would present a security risk for the whole application and that is unacceptable. 这是因为它将对整个应用程序造成安全风险,这是不可接受的。 The other caveat is that the C# program is called and started from the batch file with the START /WAIT command. 另一个警告是,使用START /WAIT命令从批处理文件中调用并启动C#程序。 To clarify, I am not the only author of this code, I made a good portion of it, debugged and unit tested it, but this code is not entirely mine. 需要说明的是,我不是该代码的唯一作者,我在其中做了很大一部分,对其进行了调试和单元测试,但是该代码并非完全属于我。 Thus "just refactor it and use something easier" is not an option this late into development. 因此,“延迟重构并使用更简单的方法”在开发后期就不是一种选择。 The code works well for its intent, I just need to solve this problem. 该代码按其意图运行良好,我只需要解决此问题即可。 These caveats, along with the fact that the C# program is NOT a console application but a windows forms application, leads me to believe this question is not a duplicate of other questions on this topic. 这些警告,以及C#程序不是控制台应用程序而是Windows Forms应用程序这一事实,使我相信此问题不是该主题上其他问题的重复。 This is because I understand how to get batch to try reading the program output (what the other questions were about) but I don't understand what to put in the C# program to allow batch to succeed in reading it (my question). 这是因为我了解如何获取批处理以尝试读取程序输出(其他问题是关于什么),但是我不了解在C#程序中放入什么内容才能使批处理成功读取(我的问题)。

What is the application for? 申请的目的是什么?

I won't go into details because that probably isn't legal but the purpose of the C# program and the batch program is basically to setup windows machines for our network service by configuring various windows options. 我不会详细介绍,因为这可能不合法,但是C#程序和批处理程序的目的基本上是通过配置各种Windows选项来为我们的网络服务设置Windows计算机。 the C# portion is needed for passwords, which can't be done in batch because there is no way to have special character passwords in a variable (batch interprets them as commands instead of literals and there is no easy way around this). C#部分是密码所必需的,因为不能在变量中使用特殊字符密码(批处理将它们解释为命令而不是文字,并且没有简便的方法),所以不能批量处理密码。 Since it is a requirement that our users be able to have special characters in their passwords, we couldn't use batch for that portion of the program. 由于要求我们的用户的密码中必须包含特殊字符,因此我们不能在该部分程序中使用批处理。 The problem is that I need to know when the C# program is abruptly closed. 问题是我需要知道C#程序何时突然关闭。 As a windows form, it has the red X option that can be selected before the password is set. 作为Windows窗体,它具有红色的X选项,可以在设置密码之前选择它。 The batch file needs to know if the C# program was closed early so it can report a warning to the user that the password was not set. 批处理文件需要知道C#程序是否提前关闭,以便可以向用户报告未设置密码的警告。 The problem is that I can't figure out how to get output from the C# application into batch to display that warning. 问题是我无法弄清楚如何将C#应用程序的输出批量显示以显示警告。 If I can't flag the warning condition, I can't display the warning. 如果我无法标记警告条件,则无法显示警告。

my research 我的研究

I found this question: Assign output of a program to a variable 我发现了这个问题: 将程序的输出分配给变量

Which gives me a good starting point. 这给了我一个很好的起点。 Instead of calling the application generically however, I used the start command because I wanted the batch file to wait before it continues on with the rest of the process. 但是,我没有使用通用命令来调用应用程序,而是使用了start命令,因为我希望批处理文件在继续进行其余过程之前要等待。 My batch code when ran from the CLI (assume I'm in the directory of the executable) is this: 从CLI运行时,我的批处理代码是这样的(假设我在可执行文件的目录中):

FOR /F "tokens=*" %A IN ('start /WAIT "" "<TheApplicationName>.exe"') DO (echo output line: "%A")

Obviously in a batch script the extra percent signs need to be added but for the CLI itself this is fine. 显然,在批处理脚本中,需要添加额外的百分号,但是对于CLI本身,这很好。

The problem is that I can't figure out what command in C# I need to execute to get something to be read by the above batch code. 问题是我无法弄清楚我需要在C#中执行什么命令才能使上述批处理代码读取某些内容。 I've tried Console.Writeline("something") in my C# main class but it didn't cause anything to echo out when I ran the program through the batch code. 我在C#主类中尝试过Console.Writeline("something") ,但在通过批处理代码运行程序时,它没有引起任何回声。 I'm pretty sure the batch code is correct because it does run the application. 我非常确定批处理代码正确,因为它确实运行了应用程序。

The steps I take to run the C# program are as follows: 我要运行C#程序的步骤如下:

  1. cd into the right directory cd进入正确的目录
  2. run the above batch code 运行上面的批处理代码
  3. check that the application runs in its entirity like it is supposed to (it does) 检查应用程序是否像预期的那样完整运行(确实如此)
  4. Observe that nothing from the program is echoed out, not even a blank line (the DO portion of the FOR loop never seems to output anything) 观察到程序中没有任何内容被回显,甚至没有空行( FOR循环的DO部分似乎从不输出任何内容)

So my question is this: what command in C# do I need to use to get the batch code to echo out SOMETHING from my program. 所以我的问题是:我需要使用C#中的哪个命令来获取批处理代码以从程序中echo某些内容。 I know how to use the string once I get it, I just need to get it first. 我知道一旦获得该字符串就该如何使用,我只需要先获得它即可。 I feel like either, it can't be done because it is a windows form application, it can be done but I'm just not smart, or that the read operation is happening too fast (before anything is printed) and thus I get nothing. 我觉得这是无法完成的,因为它是Windows窗体应用程序,可以完成,但我并不聪明,或者读取操作发生得太快(在打印任何内容之前),因此我得到了没有。 I can't find any examples of what commands the batch code can 'read' so I assumed it was somehow related to console output or print commands. 我找不到批处理代码可以“读取”哪些命令的任何示例,因此我认为它与控制台输出或打印命令有某种联系。

Suggestions for question improvement are appreciated 提出了改善问题的建议

for /f executes the code in the do clause for each non empty line (see also for /? output about eol handling) in the output of the indicated command. for /f在指示的命令的输出中为每个非空行执行do子句中的代码(另请参见关于eol处理的for /?输出)。 The problem in your code is that the output being processes is that of the start command (start command outputs nothing), not your application's output. 在你的代码的问题是,输出是过程是,的start命令(start命令输出什么),而不是你的应用程序的输出。

As the for /f will wait for the executed command to end (the code in the do clause is not executed until all the output has been retrieved) the start /wait is not only not needed but the soruce of the problems. 由于for /f将等待执行的命令结束( do子句中的代码直到检索到所有输出后才执行),因此不仅不需要start /wait ,而且可以解决问题。

Just remove the start /wait from the command to process the application's output. 只需从命令中删除start /wait即可处理应用程序的输出。

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

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